Using object types with Jasmine's toHaveBeenCalledWith method

screenm0nkey picture screenm0nkey · Jan 10, 2012 · Viewed 63.8k times · Source

I've just started using Jasmine so please forgive the newbie question but is it possible to test for object types when using toHaveBeenCalledWith?

expect(object.method).toHaveBeenCalledWith(instanceof String);

I know I could this but it's checking the return value rather than the argument.

expect(k instanceof namespace.Klass).toBeTruthy();

Answer

Wolfram Arnold picture Wolfram Arnold · Feb 2, 2012

I've discovered an even cooler mechanism, using jasmine.any(), as I find taking the arguments apart by hand to be sub-optimal for legibility.

In CoffeeScript:

obj = {}
obj.method = (arg1, arg2) ->

describe "callback", ->

   it "should be called with 'world' as second argument", ->
     spyOn(obj, 'method')
     obj.method('hello', 'world')
     expect(obj.method).toHaveBeenCalledWith(jasmine.any(String), 'world')