skip to Main Content

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 ??

2

Answers


  1. Chosen as BEST ANSWER

    Thanks, I tried it but got also an error message:

    Expected spy open to have been called with:
      [ Object({ account: ‘abc’, that: spy on EventProvider folder1.project.Controller }) ]
    
    but actual calls were:
      [ Object({ account: ‘abc’, that: EventProvider folder1.project.Controller }) ].
    

  2. 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);
      });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search