Mocking a call out, matching specific parameters but not others

7 views
Skip to first unread message

Grant Schoep

unread,
Nov 9, 2018, 9:59:22 PM11/9/18
to mocha-developer
Ok  I've been banging away it this problem for hours.

I am trying to mock out a call to a class and verify certain parameters match exactly, but not care about the others

So the class I am testing, lets call it SomethingElse

class SomethingElse
  def blah
     MyClass.new('SomeKey', 1234, Time.now) 
  end
end

So I have been doing this.

object = mock
MyClass.expects(:new).returns(object)

test_obj = SomethingElse.new
test_obj.blah

This works fine, but I want my test to verify those first two parameters on MyClass.new. I know what they must be, the third can be anything(just picking Time.now to make that clear)

I tried something like this, of course anything didn;'t work there. 

MyClass.expects(:new).with("SomeKey", 1234, anything).returns(object)

Reading more, I thought includes might help. I tried

MyClass.expects(:new).with(includes("SomeKey", 1234)).returns(object)

But I got some error on converting integer to string. I've dug and tried all sorts of other things. I figure I am missing some simple aspect of this I am missing.

I am still pretty new to Mocha, so this is probably something dead simple...

floehopper

unread,
Nov 10, 2018, 4:27:09 AM11/10/18
to mocha-developer
Hi Grant,

I hope you don't mind, but I've opened a new issue [1] on the Mocha GitHub repo to capture this.

Regards, James.

Reply all
Reply to author
Forward
0 new messages