当使用Capybara和Cucumber时,未定义的方法'应该'

shy*_*cha 4 ruby-on-rails cucumber testunit capybara ruby-on-rails-3

我试图在我的Rails应用程序中使用Capybara和Cucumber.所以,这就是我所做的:

  • 需要安装宝石并将它们添加到 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
Run Code Online (Sandbox Code Playgroud)

这是步骤定义:

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
Run Code Online (Sandbox Code Playgroud)

在运行时,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'
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?是的,我的功能有任何错误吗?

pru*_*wan 15

替换page.should have_content post.titleassert page.has_content?(post.title).如果可行,则将其应用于其他have_content语句

编辑:涉及的should那些语句基于rspec期望,并且只有在您已经在使用rspec或其他一些responseds_to的测试框架时才应该使用should.从test :: unit角度来看,这可能只是另一种assert.