[英]Undefined method 'should' when using Capybara with Cucumber
I am trying to use both Capybara and Cucumber in my Rails application. 我试图在我的Rails应用程序中使用Capybara和Cucumber。 So, that's what i did:
所以,这就是我所做的:
Gemfile
Gemfile
rails generate capybara:install --cucumber
rails generate capybara:install --cucumber
Here's my feature (yeah, i am creating blog application): 这是我的功能(是的,我正在创建博客应用程序):
Feature: Browse posts
So that I can browse through the posts
As a visitor
I want to see all and/or particular posts
Scenario: Viewing all the posts
Given Posts exist
When I navigate to the website home
Then I should see all the posts
Scenario: Viewing particular post
Given Post #1 exists
When I navigate to /posts/view/1
Then I should see the post with id=1
And here's its step definitions: 这是步骤定义:
Given /^Posts exist$/ do
assert (not Post.all.empty?), "No posts found at all"
end
Given /^Post #(\d+) exists$) do |id|
assert Post.find_by_id(id).valid?, "Post ##{ id } was not found at all"
end
When /^I navigate to (.+)$/ do |url|
if url =~ /^the website home$/ then
visit '/'
else
visit url
end
end
Then /^I should see all(.+)posts$/ do |delimiter|
posts = Post.all
posts.each do |post|
page.should have_content post.title
end
end
Then /^I should see the post with id([^\d]{1,})(\d+)$/ do |delimiter, id|
post = Post.find_by_id id
page.should have_content post.title
end
And when running rake cucumber
i get this message: 当运行
rake cucumber
我收到以下消息:
Then I should see all the posts # features/step_definitions/browsing_posts.rb:13
undefined method `should' for #<Capybara::Session> (NoMethodError)
./features/step_definitions/browsing_posts.rb:17:in `block (2 levels) in <top (required)>'
./features/step_definitions/browsing_posts.rb:16:in `each'
./features/step_definitions/browsing_posts.rb:16:in `/^I should see all(.+)posts$/'
features/browsing_posts.feature:9:in `Then I should see all the posts'
What am I doing wrong? 我究竟做错了什么? And yeah, are there any mistakes in my feature?
是的,我的功能有任何错误吗?
Replace page.should have_content post.title
with assert page.has_content?(post.title)
. 将
page.should have_content post.title
替换为assert page.has_content?(post.title)
。 If that works, apply that similarly to the other have_content
statements 如果
have_content
,则将其应用于其他have_content
语句
Edit: Those statements involving should
are based on rspec expectations , and should be used only if you are already using rspec or some other testing framework that responds_to should
. 编辑: 涉及
should
语句基于rspec期望 ,并且只有在您已经使用rspec或者其他响应should
测试框架时才应该使用。 Coming from the test::unit angle, this is probably just another kind of assert
. 从test :: unit角度来看,这可能只是另一种
assert
。
can you try 你能试一下吗
page.has_content?('foo')
instead of 代替
page.should have_content post.title page.should has_content post.title
if it works, then you can use this with assert. 如果它工作,那么你可以使用断言。
after facing the same issue I tried 面对同样的问题我试过
expect(page).to have_content('foo')
which worked fine for me 这对我来说很好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.