JavaScript Jasmine Test - Expected spy open to have been called with - Mock EventProvider

33 views Asked by At

I have the following problem:

A Code snippet from my controller is:

const account = “abc”, isIDValid = true, loadData = this.loadData, that = this;

let args = {account, isIDValid, loadData, that};

dialog.open(args);

My describe in the test is:

describe("when user click open dialog“, function() {
    let account, isIDValid, loadData, that, args;
    beforeEach(function() {
        account = “abc”;
        isIDValid = true;
        loadData = function() {};
        that = folder1.project.Controller;
        args = {account, isIDValid, loadData, that};
    });

    it("should call dialog with args, function() {
        controller.abc(isIDValid);
expect(dialog.open).toHaveBeenCalledWith(args);
    });
});

I got the following error in javascript Jasmine test:

Expected spy open to have been called with:

  [ Object({ account: 'abc', isIDValid: true, loadData: Function, that: Function }) ]

but actual calls were:

  [ Object({ account: 'abc', isIDValid: true, loadData: Function, that: EventProvider folder1.project.Controller }) ].

Do someone know how I can mock the function "loadData" and "this" -> EventProvider ??

1

There are 1 answers

0
Sahil Jariwala On

Try with spyOn function by Jasmine

    describe("when user clicks open dialog", function() {
  let account, isIDValid, loadData, that, args;

  beforeEach(function() {
    account = "abc";
    isIDValid = true;
    loadData = jasmine.createSpy("loadData");
    that = jasmine.createSpyObj("EventProvider folder1.project.Controller", ["abc"]);
    args = { account, isIDValid, loadData, that };
  });

  it("should call dialog with args", function() {
    controller.abc(isIDValid);
    expect(dialog.open).toHaveBeenCalledWith(args);
  });
});