miquel09
miquel09

Reputation: 174

Ionic unit testing alertcontroller

I am working for an application that uses an alert to tell the user it is using nfc. I am unit testing this app and put a spy on the alertcontroller.create method as follows:

alertController.create = jasmine.createSpy().and.resolveTo({
    present: jasmine.createSpy()
});

In the unit test I want to check if it is called with the right alert options as follows:

      expect(alertController.create).toHaveBeenCalledWith({
        header: 'NFC aan het lezen...',
        message: 'Hou de pas tegen de achterkant van de telefoon',
        buttons: [
          {
            text: 'Anuleren',
            role: 'cancel',
            handler: () => nfc.stopRead()
          }
        ]
      });

The problem however is that it gives an error while running the test because of the handler. How can I effectively test if the alertcontroller.create function is called with the correct values? As right now the test gives the following error:

  Expected $[0].buttons[0].handler = Function to equal Function.

I want to check if the provided object is correct but the function doesn't check.

Upvotes: 1

Views: 840

Answers (1)

miquel09
miquel09

Reputation: 174

I got it to work with

jasmine.any(Function)

So the code is as follows:

      expect(alertController.create).toHaveBeenCalledWith({
        header: 'NFC aan het lezen...',
        message: 'Hou de pas tegen de achterkant van de telefoon',
        buttons: [
          {
            text: 'Anuleren',
            role: 'cancel',
            handler: jasmine.any(Function) as any
          }
        ]
      });

Upvotes: 1

Related Questions