0

Lets suppose i have a class like this:

class Splitter
  def split args
  end
end

And another class like this one:

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

How would you test that when Text.new is called:

Splitter is called twice,one time with the argument "first" and another time with the argument "twice" in the argument array

I tried:

 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 

This test doesn't work for me because I get errors like:

   -["array_including(first)"]
   +[["first","234nnmer"],nil]

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

1 Answer 1

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

The following might also work:

array_including(array_including("first"))
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry but somehow when i try array_including(array_including("first")) it returns me a error: -["array_including(array_including(first))"] +[["first","234nnmer"],nil]
It seems like it transforms array_include to an String

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.