我想在我的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'
我在做什麼錯?是的,我的功能有什麼錯誤嗎?
這不會解決你的問題,但提高你的故事的質量:'鑑於nothing'是不正確的。 「存在帖子」更有可能是你想要的。 –
你使用默認的test :: unit還是rspec?你的一些語法更多的是rspec – prusswan
@prusswan風格,我不確定=)我用'rake cucumber'運行測試,並且沒有看到我提供的__rspec__或__test :: unit__代碼。我想黃瓜使用它自己的類 – shybovycha