ruby-on-rails - 未定义的方法 'should' 将 Capybara 与 Cucumber 一起使用时

标签 ruby-on-rails ruby-on-rails-3 cucumber capybara testunit

我正在尝试在我的 Rails 应用程序中同时使用 Capybara 和 Cucumber。所以,这就是我所做的:

  • 安装需要的 gems 并将它们添加到 Gemfile
  • 跑了rails generate capybara:install --cucumber
  • 为 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'
    

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

    最佳答案

    替换 page.should have_content post.titleassert page.has_content?(post.title) .如果可行,请将其应用于另一个 have_content声明

    编辑:那些 statements involving should 均基于 rspec expectations ,并且仅当您已经在使用 rspec 或其他一些响应于 should 的测试框架时才应使用.从 test::unit 角度来看,这可能只是另一种 assert .

    关于ruby-on-rails - 未定义的方法 'should' 将 Capybara 与 Cucumber 一起使用时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12176878/

    相关文章:

    ruby-on-rails - curl : (1) Protocol https not supported or disabled in libcurl

    java - 浏览搜索结果页面并使用 selenium webdriver 查找链接

    database - 使用 selenium 在 capybara 中创建动态记录不起作用

    ruby - 我如何使用页面对象模式清除我的 capybara 步骤?

    php格式化输出html

    mysql - 在 rails 中从 mysql 迁移到 postgresql

    ruby-on-rails - rails 测试仅在 get 请求中传递 CSRF token

    ruby-on-rails - 堆栈级别太深且 before_save

    ruby-on-rails - Rails cron 不发送电子邮件

    ruby-on-rails - Rails 开发模式下的日志轮换?