无法模拟`Resolv::DNS.open`

Trouble mocking `Resolv::DNS.open`

我正在尝试使用 MiniTest/Mocks 模拟下面的代码。但是当 运行 我的测试时,我一直收到这个错误。

Minitest::Assertion: unexpected invocation: #<Mock:0x7fa76b53d5d0>.size()
unsatisfied expectations:
- expected exactly once, not yet invoked: #<Mock:0x7fa76b53d5d0>.getresources("_F5DC2A7B3840CF8DD20E021B6C4E5FE0.corwin.co", Resolv::DNS::Resource::IN::CNAME)
satisfied expectations:
- expected exactly once, invoked once: Resolv::DNS.open(any_parameters)

正在测试代码

txt = Resolv::DNS.open do |dns|
            records = dns.getresources(options[:cname_origin], Resolv::DNS::Resource::IN::CNAME)
          end
          binding.pry
          return (txt.size > 0) ? (options[:cname_destination].downcase == txt.last.name.to_s.downcase) : false

我的测试

::Resolv::DNS.expects(:open).returns(dns = mock)
  dns.expects(:getresources)
     .with(subject.cname_origin(true), Resolv::DNS::Resource::IN::CNAME)
     .returns([Resolv::DNS::Resource::IN::CNAME.new(subject.cname_destination)])
     .once

现在您正在测试 Resolv::DNS 收到 open return 您的 mock 但是 由于您似乎在尝试测试 dns 模拟是否正在接收消息,因此您需要存根该方法并为其提供要生成的对象

试试这个:

 dns = mock 
 dns.expects(:getresources)
   .with(subject.cname_origin(true), Resolv::DNS::Resource::IN::CNAME)
   .once
 ::Resolv::DNS.stub :open, [Resolv::DNS::Resource::IN::CNAME.new(subject.cname_destination)], dns do 
   # whatever code actually calls the "code being tested" 
 end 
 dns.verify

stub 的第二个参数是存根的 return 值,stub 的第三个参数是将生成的块代替原始生成的值。

在 RSpec 中,语法更简单(也更语义化),因此:

 dns = double 
 allow(::Resolv::DNS).to receive(:open).and_yield(dns) 
 expect(:dns).to receive(:getresources).once
   .with(subject.cname_origin(true), Resolv::DNS::Resource::IN::CNAME)
   .and_return([Resolv::DNS::Resource::IN::CNAME.new(subject.cname_destination)])
  # whatever code actually calls the "code being tested" 

您可以使用 DnsMock 编写更具可读性的集成测试,而不是 stubbing/mocking 部分代码:https://github.com/mocktools/ruby-dns-mock