Cucumber中的Rspec期望:在类中使用未定义的方法“be”

Cucumber中的Rspec期望:在类中使用未定义的方法“be”,rspec,cucumber,capybara,rspec-expectations,Rspec,Cucumber,Capybara,Rspec Expectations,我在cucumber框架中使用rspec期望值,在步骤定义级别使用时,它们看起来很好 我已将env.rb文件配置为: require 'rspec/expectations' World(RSpec::Matchers) 我现在注意到的问题是,如果我试图在某个步骤中使用的对象的方法中使用rspec,那么我就失败了 E.g. Steps_definition.rb service.use_rspec class Service def use_rspec header

我在cucumber框架中使用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…类似的断言,在该类中应该有“…”内容,并且“have\u content”也无法识别,因此不确定发生了什么:s


谢谢你给我小费

您的服务类不在世界上,因此RSpec::Matchers在那里不可用

你有两种可能:

将RSpec::Matchers手动包含到此类中。 将此类或模块放入World。之后,其方法将在步骤定义中直接可用。 写:

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


嗨,安德烈。我不知道我的问题在哪里。我已经创建了一个模块,我把它放到了我创建服务对象的地方,所以服务方法在步骤中是可用的,这很好。在尝试了上面的建议之后,我遇到的问题是,在类中似乎没有识别出Capybara和Rspec::Matcher。如果我尝试:在类中包含RSpec::Matchers,那么我会得到以下错误未定义方法new'forrspec::Matchers::Pretty:moduleerror创建格式化程序:Pretty NoMethodError`@mickael它适合我。试着从头开始。水豚和RSpec::Matchers在模块或类中是可用的,如果它被放到World中,也许您需要WorldRSpec::Expections?
module Helpers
  def method
    # Capybara and RSpec::Matchers are available here
  end
 end
World(Helpers)