[RAILS3.1] Capybara does not find the text that is actually there!

This was originaly posted on stackoverflow (<rspec2 - Capybara does not find the text that is actually there! - Stack Overflow)

It would be nice if answers could be there to, if not I will summarize the helping answers and post them there.

---8<--- SO Post beginns here:

In my actual project I use the following gems for testing:

* capybara from git://github.com/jnicklas/capybara.git in revision 6641fddcfc337a3ddaa84ac59272e884090332c3 * rails (3.1.0.rc5) (and its requirements) * factory_girl (2.0.1) * factory_girl_rails (1.1.0) * rspec (2.6.0) * rspec-core (2.6.4) * rspec-rails (2.6.1)

When doing `rake spec` I get the following error:

    ...F.....*................*....*.*.*.

    Pending:       <pending snipped out>

    Failures:

      1) Articles GET /articles/:id should show the article when clicking it          Failure/Error: page.should have_content a.body            expected there to be content "Dieser Artikel ist nur zum testen, erfüllt keinen Sinn und langweilig ist mir ohnehin. Das sollte nur mal so gesagt werden... MfG Euer Admin!\n" in "Stars3\n \n Stars!Artikel - 1 - This is an article created just for testing purpose\n \n \n Artikel\n\n \nDieser Artikel ist nur zum testen, erfüllt keinen Sinn und langweilig ist mir ohnehin. Das sollte nur mal so gesagt werden... MfG Euer Admin!\n\n \n"          # ./spec/requests/articles_spec.rb:46

    Finished in 1.96 seconds     37 examples, 1 failure, 5 pending

    Failed examples:

    rspec ./spec/requests/articles_spec.rb:40 # Articles GET /articles/:id should show the article when clicking it

I cant see any differences between the expectation and the result... Can you point me in the right direction to make this one work?

The spec:

<!-- language: ruby -->

    describe "GET /articles/:id" do       it "should show the article when clicking it", :type => :request do         a = Factory.create(:article)#, :user => u)         a.save         visit articles_path         click_link a.title         page.should have_content a.title         page.should have_content a.body       end     end

The factory:

<!-- language: ruby -->

    Factory.define :article do |a|       Factory.sequence :title do |i|         "#{1} - This is an article created just for testing purpose"       end

      a.title { Factory.next(:title) }       a.association :user, :factory => :user #user { User.first }       a.body <<eot     Dieser Artikel ist nur zum testen, erfüllt keinen Sinn und langweilig ist mir ohnehin. Das sollte nur mal so gesagt werden... MfG Euer Admin!     eot

    end

The view:

<!-- language: ruby -->

    <% title "Artikel - #{@article.title}" -%>

    <%= @article.compiled -%>

The controller:

<!-- language: ruby -->

    class ArticlesController < ApplicationController       before_filter :login_required, :except => [:index, :show]

      def show         @article = Article.find_by_title(params[:id])       end

    end

And the model:

<!-- language: ruby -->

    class Article < ActiveRecord::Base       validates_presence_of :body, :message => "Es muss schon Text drin stehen"       validates_presence_of :title, :message => "Du brauchst nen Titel für den Artikel"       validates_presence_of :user, :message => "Das hätte nicht passieren dürfen... Es wurde kein Nutzer angegeben"       belongs_to :user     # before_save :compile_body

      def compiled         body       end

      def to_param         "#{title}"       end

    end

If something is missing, please feel free to ask, I will put it here.

--->8--- SO post ends here

TIA Norbert

I suspect the problem may be around the newlines and whitespace. Are you sure they match exactly? Try first changing the test and factory to provide a simple string and work up from there. Do you really need such a complex example text anyway? If yes then use a regular expression and ignore the whitespace details.

Colin

OK, setting the body a oneliner does the test pass...

Thank you!