2011-07-27 44 views
1

在我的實際項目中,我使用下面的寶石來進行測試:水豚沒有找到實際存在的文本!

  • 從git的水豚://github.com/jnicklas/capybara.git在修訂6641fddcfc337a3ddaa84ac59272e884090332c3
  • 軌(3.1.0.rc5)(及其需求)
  • factory_girl(2.0.1)
  • factory_girl_rails(1.1.0)
  • rspec的(2.6.0)
  • rspec的核(2.6.4)
  • RSpec的護欄(2.6.1)

在做rake spec我得到以下錯誤:

...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 

我不能看到的期望和結果之間的差異...你能指出我在正確的方向,使這一個工作?

該規範:

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 

工廠:

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 

的視圖:

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

<%= @article.compiled -%> 

控制器:

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

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

end 

和模型:

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 

如果缺了點什麼,請隨時問,我會把它放在這裏。

+0

真的沒人能告訴我嗎? – NobbZ

回答

1

通過改變工廠文章正文設置爲一個簡單的oneliner到

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 

並測試合格使。