使用水豚黄瓜时未定义的方法'应该'
我想在我的Rails应用程序中同时使用水豚和黄瓜。所以,这就是我所做的:使用水豚黄瓜时未定义的方法'应该'
- 安装宝石需要,并将它们添加到
Gemfile
- 跑
rails generate capybara:install --cucumber
- 创建要素和黄瓜其步骤定义
这是我的特点(是的,我正在创建博客应用程序):
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
而这里是其步骤定义:
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
运行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'
我在做什么错?是的,我的功能有什么错误吗?
回答:
将替换为assert page.has_content?(post.title)
。如果这样的作品,同样适用的是其他have_content
陈述
编辑:那些statements involving should
基于rspec expectations,如果您已经使用RSpec的或其他一些测试框架,responds_to should
只应使用。来自test :: unit角度,这可能只是另一种assert
。
回答:
可以尝试
page.has_content?('foo')
,而不是
page.should have_content post.title
如果它工作,那么你就可以断言使用。
回答:
面临着同样的问题后,我试图
expect(page).to have_content('foo')
它工作得很好,我
以上是 使用水豚黄瓜时未定义的方法'应该' 的全部内容, 来源链接: utcz.com/qa/264572.html