Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test that the method has been called in jasmine?

I'm a bit confused with spying on jasmine. I have a code like this, but I'm not sure how to test it.

var params = {
    param1: "",
    param2: "link",
    param3: "1", 
    param4 : "1"
};
var func = new myFunction(params );
func.doSomething();

How do test that func.doSomething has been called.

This is the test I have written so far

describe("Library", function() {

  beforeEach(function() {
  });

  it("should include correct parameters", function() {
      expect(params.param1).toEqual("");
      expect(params.param2).toEqual("link");
      expect(params.param3).toEqual("1");
      expect(params.param4).toEqual("1");
  });

  it("should show that method doSomething is called with zero arguments", function() {
          // I'm not sure how to write test for this.
  });
});
like image 921
toy Avatar asked May 30 '12 13:05

toy


2 Answers

I think you want to use toHaveBeenCalledWith():

it("should show that method doSomething is called with zero arguments", function() {
    // Ensure the spy was called with the correct number of arguments
    // In this case, no arguments
    expect(func.doSomething).toHaveBeenCalledWith();
});
like image 163
Colin Brock Avatar answered Oct 24 '22 07:10

Colin Brock


If spy function has been called exactly once, use toHaveBeenCalledOnceWith matcher:

expect(mySpy).toHaveBeenCalledOnceWith('', 'link', "1", "1");

It combines toHaveBeenCalledTimes(1) and toHaveBeenCalledWith() matchers.

Coming with Jasmine 3.6.

like image 27
t_dom93 Avatar answered Oct 24 '22 07:10

t_dom93