简体   繁体   English

从不同的文件调用 rspec 方法

[英]Calling rspec methods from different file

I am trying to write a class in my code to wrap some of the RSpec calls.我正在尝试在我的代码中编写一个 class 来包装一些 RSpec 调用。 However, whenever I try to access rspec things, my class simply doesn't see the methods.但是,每当我尝试访问 rspec 的东西时,我的 class 根本看不到这些方法。

I have the following file defined in spec/support/helper.rb我在spec/support/helper.rb中定义了以下文件

require 'rspec/mocks/standalone'

module A
  class Helper
    def wrap_expect(dbl, func, args, ret)
      expect(dbl).to receive(func).with(args).and_return(ret)
    end
  end
end

I get a NoMethodError: undefined method 'expect' , despite requiring the correct module.我得到一个NoMethodError: undefined method 'expect' ,尽管需要正确的模块。 Note that if I put calls to rspec functions before the module, everything is found correctly.请注意,如果我在模块之前调用 rspec 函数,一切都会正确找到。

I've tried adding the following like to my spec_helper.rb :我尝试将以下内容添加到我的spec_helper.rb

  config.requires << 'rspec/mocks/standalone'

But to no avail.但无济于事。

I managed to use class variables in my class and passing the functions through from the global context, but that solution seems quite extreme.我设法在我的 class 中使用 class 变量并从全局上下文传递函数,但该解决方案似乎非常极端。 Also I was able to pass in the test context itself and storing it, but I'd rather not have to do that either.我也能够传入测试上下文本身并存储它,但我也不想这样做。

expect functions by default is associated with only rspec-core methods like it before .默认情况下, expect函数只与it类似before rspec-core 方法相关联。 If you need to have expect inside a method, you can try adding the Rspec matcher class in the helper file.如果您需要在方法中包含期望,您可以尝试在帮助文件中添加 Rspec 匹配器 class。

include RSpec::Matchers

that error because the self which call expect is not the current rspec context RSpec::ExampleGroups , you could check by log the self该错误是因为调用expectself不是当前 rspec 上下文RSpec::ExampleGroups ,您可以通过记录self来检查

module A
  class Helper
    def wrap_expect(dbl, func, args, ret)
      puts self
      expect(dbl).to receive(func).with(args).and_return(ret)
    end
  end
end

# test case
A::Helper.new.wrap_expect(...) # log self: A::Helper

so obviously, A::Helper does not support expect很明显,A::Helper 不支持expect

now you have 2 options to build a helper: (1) a module or (2) a class which init with the current context of test cases:现在您有 2 个选项来构建帮助程序:(1)一个模块或(2)一个 class,它使用当前的测试用例上下文进行初始化:

(1) (1)

module WrapHelper
 def wrap_expect(...)
   puts self # RSpec::ExampleGroups::...
   expect(...).to receive(...)...
 end
end

# test case
RSpec.describe StackOverFlow, type: :model do
  include WrapHelper
  it "...." do
    wrap_expect(...) # call directly 
  end
end

(2) (2)

class WrapHelper
 def initialize(spec)
   @spec = spec 
 end
 
 def wrap_expect(...)
   puts @spec # RSpec::ExampleGroups::...
   @spec.expect(...).to @spec.receive(...)...
 end
end

# test case
RSpec.describe StackOverFlow, type: :model do
  let!(:helper) {WrapHelper.new(self)}
  
  it "...." do
    helper.wrap_expect(...)
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM