【问题标题】:How to use rspec-expectations outside cucumber step-definitions如何在黄瓜步骤定义之外使用 rspec-expectations
【发布时间】:2012-10-23 21:23:27
【问题描述】:

我正在使用 watir-cucumber 进行自动化测试。我在单独的.rb 中编写了以下方法,此方法不在步骤定义中。

 def has_logged_in?
   $browser.text.should include("t788")
 end

当我从步骤定义中调用此方法时,会出现此错误,

 wrong argument type String (expected Module) (TypeError)

相同的代码在步骤定义中也能正常工作。我四处搜索,发现include方法用于包含模块,但这是ruby-include方法,should includerspec\expectations下。那么如何在上面的步骤定义之外调用should include 方法。 我在 linux 上使用 watir-cucumber

【问题讨论】:

  • 你在哪里以及为什么写这个方法?
  • text.should =~ /t788/ 也可以
  • @IsmaelAbreu 我想测试用户是否已登录,因此我不想为每个步骤都编写它,而是要调用此函数,然后它将决定步骤是通过还是失败。这是为了避免代码重复

标签: ruby rspec cucumber watir watir-webdriver


【解决方案1】:

您想要的include 方法在RSpec::Matchers 模块中。

如果你的 has_logged_in? 方法在一个类中(不是 main 的一部分),你可以在你的类中包含 RSpec::Matchers 模块。这将使您能够访问include 方法。

所以你的班级看起来像:

class YourClass
    include RSpec::Matchers

    def has_logged_in?
        $browser.text.should include("t788")
    end
end

注意:我以前不必这样做,但通过快速检查,只要 RSpec::Matchers 包含在类中而不是主类中,它就可以工作。在 main 中包含它似乎没有做任何事情(即 include 继续调用标准的 include 模块方法)。我没有探索这样做是否有任何负面影响。

【讨论】:

  • @UdaySwami 和你之前看到的一样吗?你能分享你的整个 .rb 文件吗?
【解决方案2】:

在您的 gem 文件中:

gem 'rspec', "1.3.2", :require => "spec/expectations"

或者在 RSpec 1.X.X 的 env.rb 中:

require 'spec/expectations'

或者在 RSpec 2.X 的 env.rb 中:

require 'rspec/expectations'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2014-01-10
    • 2020-07-12
    • 1970-01-01
    相关资源
    最近更新 更多