Verifying page title with rspec

后端 未结 4 1041
孤街浪徒
孤街浪徒 2021-01-05 00:05

I\'m running through Michael Hartl\'s Rails Tutorial.

I\'m trying to verify the title of my page. The test looks like this:

it \"should have the rig         


        
相关标签:
4条回答
  • 2021-01-05 00:18

    I strongly suspect you have a typo somewhere.

    As of this instant, I'm working through Exercise 4 of Chapter 11. Every, single problem I've had with the tutorial turned out to be a typo on my part.

    Note: I'm not cutting and pasting. I'm punching in all the code by hand.

    0 讨论(0)
  • 2021-01-05 00:21

    I found it easier to move to Capybara (I'm using Rails 3.0.1, Rspec 2.0.1, Ruby 1.9.2). Now you can do something like

    page.should have_css('title', :text => 'Ruby on Rails Tutorial Sample App | Home')
    
    0 讨论(0)
  • 2021-01-05 00:22

    Controller specs don't normally render the complete view, since they're intended to test controllers in isolation. You can tell Rspec to render the whole page by including the directive integrate_views at the top of the example group:

    describe MyController do
      integrate_views
    

    However you should ask yourself if you really want to do this, or if it would make more sense to write view specs.

    btw you can also use the CSS3 selector syntax to save a few keystrokes (might need to include the Webrat matchers for this):

    response.should have_selector("title:contains('Ruby on Rails Tutorial Sample App | Home')")
    

    EDIT

    For Rspec2, replace integrate_views with render_views

    0 讨论(0)
  • 2021-01-05 00:29

    This question looks old, which explains why there is now a better way.

    The following works well for me:

    it "has the correct page title", js: true do
      visit "/users/sign_in" # for example
      expect(page.title).to include "Sign In or Create a New Account"
    end
    
    0 讨论(0)
提交回复
热议问题