Rails integration test against page modification hack?

一曲冷凌霜 提交于 2019-12-05 12:13:16

Capybara author jnicklas confirmed here that Capybara cannot make an app do things that are not available from the UI. He recommends controller tests for authorization.

However request specs written in RSpec without using Capybara syntax do allow direct use of HTML verbs (and some additional helpers) as outlined in the RSpec and Rails docs. So rather than Capybara's fill_in and click_link directives and the page object, you can use an attribute hash, verbs like get, post, post_via_redirect, and the response.body object. It's similar to a controller test, but you're using Rails' routing to choose the appropriate controller action based on the path provided. Here is an example of the latter technique:

describe "when standard user attempts to create account_admin user" do

  let(:standard_user) { FactoryGirl.create(:standard_user) }

  let(:attr) { { email: "account_admin@example.com",
                 password: "password",
                 password_confirmation: "password",
                 role: "account_admin" }
              }

  before do
    login_as standard_user
    get new_user_path
  end

  it "should not create a account_admin user" do
    lambda do
      post users_path, user: attr
    end.should_not change(User, :count)
  end

  describe "after user posts invalid create" do
    before { post_via_redirect users_path, user: attr }

    # redirect to user's profile page
    it { response.body.should have_selector('title', text: 'User Profile') }
    it { response.body.should have_selector('div.alert.alert-error', text: 'not authorized') }
  end

end  
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!