2013-07-09 61 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。

相关问题