testing with specs, capybara from railstutorial chapter 3 does not work (have_selector('title', :text => ' | Home'))

后端 未结 6 1363
日久生厌
日久生厌 2021-02-20 14:47

im working on ruby.railstutorial.org/ruby-on-rails-tutorial-book. Im using rails 3.2.7, spork, rspec, capybara, launchy and some guards :)

i have a really weird problem

相关标签:
6条回答
  • 2021-02-20 15:08

    If you're using Capybara 2, you can use the have_title method like this:

    page.should have_title("Ruby on Rails Tutorial Sample App | Home")

    0 讨论(0)
  • 2021-02-20 15:08

    Can you test with this :

    page.should have_selector('head title',
                              :text => "Ruby on Rails Tutorial Sample App | Home")
    
    0 讨论(0)
  • 2021-02-20 15:11

    This is new to me too after putting it off for so long. Thanks to stackoverflow I was able to fix some tests with render_views in the top of the spec file right after the description. I get passing tests from

     describe "GET 'home'" do
       it "returns http success" do
         get 'home'
         response.should be_success
     end
    

    But without using layout yet I get this error for :text Capybara::ExpectationNotMet: expected to find css "title" with text "Home | Ruby on Rails Tutorial Sample App" but there were no matches. But aside from that after using gem 'capybara', '1.1.2' in the gem file my tests pass. But I also use get 'home' as this in the routes as an action match. I hope this helps.

    0 讨论(0)
  • 2021-02-20 15:20

    I just checked your project on Github, and it seems that you are using the edge version of capybara:

    Gemfile

    gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
    

    Gemfile.lock

    GIT
      remote: git://github.com/jnicklas/capybara.git
      revision: e561d249555195cdd0e9251246fc75aae876f833
      specs:
          capybara (2.0.0.beta2)
          mime-types (>= 1.16)
          nokogiri (>= 1.3.3)
          rack (>= 1.0.0)
          rack-test (>= 0.5.4)
          selenium-webdriver (~> 2.0)
          xpath (~> 1.0.0.beta1)
    

    If nothing else, I would dare say this is the cause of your problem. (And sure enough, my reference project is broken all over after updating capybara to use the edge version)

    So, unless you are keen to help with the beta testing of Capybara 2, you would do well to stick to the 1.1.2 version as listed here.

    0 讨论(0)
  • 2021-02-20 15:20

    For the moment the easiest way to do this is to access the Title element from the page html, this will return the title:

    page.html.match(/<title>(.*)<\/title>/)[1].to_s
    

    Not ideal as it's a using a regex on html(!!) but will work. Probably best to write a helper function

    # put in spec/support/utilities.rb
    def page_title(page)
      page.html.match(/<title>(.*)<\/title>/)[1].to_s
    end
    

    then your tests can be written as

    page_title(page).should eq( "Ruby on Rails Tutorial Sample App | Home" }
    
    0 讨论(0)
  • 2021-02-20 15:29

    It's just because <title> is in the <head> so it doesn't appear.

    Using this, solved my problem:

    page.should have_selector 'title', :visible => false
    

    Tip: You can apply the same to check other <head> elements, like <meta>, for example, to be sure you're (and you'll stay) Google-friendly.

    0 讨论(0)
提交回复
热议问题