Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I ended up going through the arduous process of splitting up my request and feature specs after I finished The Rails Tutorial and upgraded my Sample App to Capybara 2.0. Since you say you're still currently doing the tutorial, I would advise you to just keep with the gems that Hartl specifies (Capybara 1.1.2), finish your Sample App, and then go back to the requests/features issue as a refactoring exercise. For your reference though, this is how I ended up writing my "wrong user" authorization specs:</p> <p><strong>spec/support/utilities.rb</strong></p> <pre><code>def sign_in_through_ui(user) fill_in "Email", with: user.email fill_in "Password", with: user.password click_button "Sign In" end def sign_in_request(user) post session_path(email: user.email, password: user.password) cookies[:remember_token] = user.remember_token end RSpec::Matchers::define :have_title do |text| match do |page| Capybara.string(page.body).has_selector?('title', text: text) end end </code></pre> <p><strong>spec/features/authentication_pages_spec.rb</strong></p> <pre><code>describe "Authentication on UI" do subject { page } # ... describe "authorization" do # ... context "as a wrong user" do let(:user) { FactoryGirl.create(:user) } let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") } before do visit root_path click_link "Sign In" sign_in_through_ui(user) end context "visiting Users#edit" do let(:page_title) { full_title("Edit User") } before { visit edit_user_path(wrong_user) } it { should_not have_title(page_title) } end end end end </code></pre> <p><strong>spec/requests/authentication_requests_spec.rb</strong></p> <pre><code>describe "Authentication Requests" do subject { response } # ... describe "authorization" do # ... context "as a wrong user" do let(:user) { FactoryGirl.create(:user) } let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") } before { sign_in_request(user) } context "PUT Users#update" do before { put user_path(wrong_user) } it { should redirect_to(root_url) } end end end end </code></pre> <p>I primarily used the following two links as reference when trying to figure out how to separate my <code>feature</code> specs from my <code>request</code> specs:</p> <ul> <li><a href="http://alindeman.github.com/2012/11/11/rspec-rails-and-capybara-2.0-what-you-need-to-know.html" rel="nofollow">rspec-rails and capybara 2.0: what you need to know</a></li> <li><a href="https://github.com/rspec/rspec-rails/blob/master/Capybara.md" rel="nofollow">rspec-rails Capybara page</a></li> </ul> <h3>Update:</h3> <p>If you don't want the custom RSpec matcher, you can also use the following in the tests above to get the same result on the <code>title</code> element:</p> <pre><code>its(:source) { should have_selector('title', text: page_title) } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload