1
votes

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:

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:

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:

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

<%= @article.compiled -%>

The controller:

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

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

end

And the model:

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.

1

1 Answers

1
votes

Setting the article body to a simple oneliner by changing the factory to

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
  a.body { "Dieser Artikel ist ein Test" }

end

did the test make passing.