24

I am using Jasmine, and I want to test whether a object has a certain method or not, like below:

it "should have 'open' method", ->
    @xhr = ajax.create()
    expect(@xhr).toHaveMethod "open" # I want to test like this!

How can I test? Thanks for your kindness.

6 Answers 6

31

@david answered it correctly. toBeDefined() is probably what you want. However if you want to verify that it is a function and not a property then you can use toEqual() with jasmine.any(Function). Here is an example: http://jsbin.com/eREYACe/1/edit

26

There isn't a built-in way to do it, but you can achieve the desired result by doing this.

it "should have 'open' method", ->
    @xhr = ajax.create()
    expect(typeof @xhr.open).toBe("function")

Do note that testing if it's defined as proposed in the other answer has some edge cases where it will pass and it shouldn't. If you take the following structure, it will pass and it's definitely not a function.

{ open : 1 }
1
  • The problem with this solution is that if the expectation fails, the message will be something like Expected undefined to be function, which isn't all that helpful. Something like Expected object '@xhr' to have property 'open' would be better.
    – user8681
    Commented Jul 21, 2015 at 11:19
11

I would try:

it "should have 'open' method", ->
    @xhr = ajax.create()
    expect(@xhr.open).toBeDefined()

see this fiddle.

2

I did it like this. Angular example:

beforeEach(inject(function ($injector) {
    service = $injector.get("myService");       
}));

it("should have myfunction function", function () {
    expect(angular.isFunction(service.myfunction)).toBe(true);
});
1
0

Jasmine allows you to write your own "matchers". The documentation explains it. http://jasmine.github.io/2.0/custom_matcher.html

You could write a very specific matcher that is called

expect(obj).toHaveMethod("methodName");

Personally I would write something a bit more generic which checks the value type. That way I could use it to not just check if a method is defined on and object/instance, but anything that can store a value. It also gives me the possibility to check for types other then the function type.

expect(obj.methodName).toBeA(Function);

To get it to work you do have to make sure to add the toBeA "matcher".

beforeEach(function(){
    jasmine.addMatchers({
      toBeA: toBeA
    });
});


function toBeA() {
  return {
    compare: function (value, type) {
      var result = {pass: val != null && val.constructor === type || val instanceof type};

      if (result.pass) {
        result.message = 'Expected ' + value + ' to be a ' + type.name
      } else {
        result.message = 'Expected ' + value + ' to not be a ' + type.name
      }

      return result;
    }
  };
}
-1

I tried this solution and it works :

spyOn(@xhr,"open")

If there is not function open, it would throw Error because it can't start spyig on it

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.