2013-07-09 58 views
0

我正在使用rspec來驗證頁面中是否存在字符串。我是新來的鐵軌和以下教程http://ruby.railstutorial.org/chapters/static-pages#top 有什麼我失蹤?即使條件滿足,rspec也會顯示失敗

require 'spec_helper' 

describe "Static pages" do 

describe "Home page" do 

it "should have the content 'Sample App'" do 
    visit '/static_pages/home' 
    page.should have_content('Sample App') 
end 
end 
end 

在我的主頁app/views/static_pages/home.html.erb我有以下代碼。

<h1>Sample App</h1> 

命令我正在使用來驗證條件。

bundle exec rspec spec/requests/static_pages_spec.rb 

測試失敗不知道爲什麼。

←[31mF←[0m 

Failures: 

1) Static pages Home page should have the content 'Sample App' 
←[31mFailure/Error:←[0m ←[31mvisit '/static_pages/home'←[0m 
←[31mNoMethodError←[0m: 
    ←[31mundefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_1:: 
Nested_1:0x4067a60>←[0m 
←[36m  # ./spec/requests/static_pages_spec.rb:8:in `block (3 levels) in                  <top(required)>'←[0m 

Finished in 0.023 seconds 
←[31m1 example, 1 failure←[0m 

Failed examples: 

←[31mrspec ./spec/requests/static_pages_spec.rb:7←[0m ←[36m# Static pages Home page should have the content 'Sample App'←[0m 

Randomized with seed 44188 
+0

看起來像'visit'是未定義的。你在你的gemfile中使用Rspec和Capybara? – Michal

回答

0

由什麼教程有你這樣做,並沒有你Gemfile仔細檢查去,我相信你正在經歷的visit關鍵字水豚2.0和RSpec之間的衝突。解決方案有兩種選擇(不需要同時執行這兩種操作):

選項1:將您的測試從spec/requests文件夾移動到spec/features文件夾中。

選項2:使用關鍵字feature代替describe

這個衝突是由於在這個要點(第二段)頂部指出的水豚的變化引起的:https://gist.github.com/jnicklas/3980553。引用:

值得注意的是,我們將Capybara假定您的規格在RSpec下運行的:類型更改爲:feature(之前爲:request)。規格/功能的最新版本。或者,您可以使用水豚功能DSL(功能而不是描述),它應該不需要任何額外的調整。如果你看到像未定義方法訪問的錯誤,那麼你可能遇到了這個問題。如果您將模塊包含到:請求規格中,則可能需要將其更改爲:feature。

相關問題