如何在黄瓜步骤定义之外使用rspec-expectations

Uda*_*ami 1 ruby rspec watir cucumber watir-webdriver

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

 def has_logged_in?
   $browser.text.should include("t788")
 end
Run Code Online (Sandbox Code Playgroud)

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

 wrong argument type String (expected Module) (TypeError)
Run Code Online (Sandbox Code Playgroud)

相同的代码在步骤定义中效果很好。我四处搜索,发现该include方法用于包含模块,但这是ruby-includemethod并should include在下面rspec\expectations。因此,如何在should include上述步骤定义之外调用方法。我在Linux上使用watir-cucumber

Jus*_* Ko 5

所需的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
Run Code Online (Sandbox Code Playgroud)

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