【问题标题】:Cucumber undefined methods黄瓜未定义方法
【发布时间】:2012-06-17 19:57:44
【问题描述】:

我正在使用 cucumbercapybara

我有几个类似的错误。

对于步骤:

Then /I should see movies of rating 'PG' or 'R'/ do
  page.body.should match(/<td>PG<\/td>/)
  page.body.should match(/<td>R<\/td>/)
end

黄瓜错误:

undefined method `match' for #<Cucumber::Rails::World:...> (NoMethodError)
./features/step_definitions/movie_steps.rb:37:in 
   `/I should see movies of rating 'PG' or 'R'/'

对于步骤:

Then /I should see an empty table/ do
  page.body.scan(/<tr>/).length.should == 0
end

黄瓜错误:

undefined method `should' for 1:Fixnum (NoMethodError)
./features/step_definitions/movie_steps.rb:46:in 
      `/I should see an empty table/'

对于步骤:

Then /I should see all of the movies/ do
  Movie.find(:all).length.should page.body.scan(/<tr>/).length
end

undefined method `should' for 10:Fixnum (NoMethodError)
./features/step_definitions/movie_steps.rb:59:in 
    `/I should see all of the movies/'

整个步骤文件是here

如您所见,这些错误非常相似,但我无法理解导致此问题的原因。

【问题讨论】:

    标签: ruby-on-rails rspec cucumber rspec-expectations


    【解决方案1】:

    在我看来,您的 rspec-expectations 有问题。尝试添加

    require 'rspec/expectations'
    

    到你的 env.rb 和你的 Gemfile 的等价物。

    【讨论】:

      【解决方案2】:

      如果您使用 MiniTest 而不是 Rspec,请尝试 assert

      assert page.body.scan(/<tr>/).length == 0, "Expected to not find <tr> in page. "
      

      【讨论】:

        【解决方案3】:

        只是为了提供替代方案(认为@dabai 的解决方案可能是最好的),您可以使用断言代替,例如:

        assert Movie.find(:all).length == page.body.scan(/<tr>/).length
        

        【讨论】:

          猜你喜欢
          • 2011-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多