【问题标题】:Rspec expectations in Cucumber: undefined method 'be' when used inside a classCucumber 中的 Rspec 期望:在类中使用时未定义的方法“be”
【发布时间】:2012-12-15 13:46:06
【问题描述】:

我在黄瓜框架中使用 rspec 期望,在步骤定义级别使用时它们看起来很好。

我已经配置了我的 env.rb 文件:

require 'rspec/expectations'
World(RSpec::Matchers)

我现在注意到的问题是,如果我尝试在某个步骤中使用的对象的方法中使用 rspec,那么我会失败。

E.g.
Steps_definition.rb
   service.use_rspec

class Service
   def use_rspec
       header = page.find("div#services h2").text
       header.should (be 'TV')
   end
 end

Error after execution:
 undefined method `be' for #<Service:0x2592570> (NoMethodError)

知道问题出在哪里吗?

我已经尝试在该类中使用 Capybara.page.find(...).should have_content('...') 进行类似的断言,并且也无法识别 'have_content',所以不确定发生了什么:S

非常感谢任何提示!

【问题讨论】:

  • 也许你需要 World(RSpec::Expectations) ?

标签: rspec cucumber capybara rspec-expectations


【解决方案1】:

您的 Service 类不在 World 中,因此 RSpec::Matchers 在那里不可用。

你有两种可能:

  1. 手动将 RSpec::Matchers 包含到此类中。
  2. 把这个类(或模块)放到 World 中。之后,其方法将在步骤定义中直接可用。

写:

class Helpers
  def method
    # Capybara and RSpec::Matchers are available here
  end
end
World{Helpers.new}

module Helpers
  def method
    # Capybara and RSpec::Matchers are available here
  end
 end
World(Helpers)

【讨论】:

  • 你好安德烈。不知道我的问题在哪里。我已经创建了一个模块,我把它放到了我创建服务对象的 World 中,所以服务方法在步骤中是可用的,这很好。我遇到的问题(即使在尝试了上述建议之后)是 #Capybara 和 Rspec::Matchers 似乎无法在课堂内被识别。如果我尝试:include RSpec::Matchers 在课堂内然后我收到以下错误undefined method new' for RSpec::Matchers::Pretty:Module 创建格式化程序时出错:pretty (NoMethodError)`
  • @mickael 它对我有用。尝试从头开始。如果将 Capybara 和 RSpec::Matchers 放入 World 中,则可以在模块或类中使用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
相关资源
最近更新 更多