模拟方法获取参数array_include

时间:2015-03-28 09:49:22

标签: ruby-on-rails ruby rspec

假设我有这样的class

class Splitter
  def split args
  end
end

另一类像这样的人:

class Text
  def initialize
     Splitter.new.split([["first","234nnmer"],nil])
     Splitter.new.split([["second","ewfr32$"],nil])
  end
end

如果<{1}}被调用,你将如何测试

Splitter被调用两次,一次使用参数Text.new,另一次使用参数数组中的参数"first"

我试过了:

"twice"

此测试对我不起作用,因为我收到如下错误:

 it "calls Splitter with arguments 'first' and 'second'" do
     expect_any_instance_of(Splitter).to receive(
            :split).with(array_including("first"))

     expect_any_instance_of(Splitter).to receive(
            :split).with(array_including("second"))

     Text.new
 end 

链接:https://github.com/rspec/rspec-mocks#argument-matchers

1 个答案:

答案 0 :(得分:1)

expect_any_instance_of(Splitter).
to receive(:split).with([array_including("first"), nil]).once

expect_any_instance_of(Splitter).
to receive(:split).with([array_including("second"), nil]).once

以下内容也可能有效:

array_including(array_including("first"))