0

I have been tasked with writing unit tests for some AngularJS code that was written by another team, who didn't write any tests

They have written the following function but I cannot figure out how to test it

function showCallAlerts(callRecord, isInEditMode, callBack) {
    var callAlerts = populateCallAlertOnEditCall(callRecord.callAlert);
    var callModalInstance = openAlertModalInstance('Call', callAlerts, callBack);
    if (callModalInstance !== undefined && callModalInstance !== null) {
    callModalInstance.result.then(function() {
        // Show equipment alerts based on company details
        showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
    });
    } else {
    // Show equipment alerts based on company details
    showEquipmentAlertsBasedOnCompanyDetails(callRecord, isInEditMode, callBack);
    }
}

I need to test that each of the functions are called, not worrying about what they do as I'll test them separate, just that they are called.

When populateCallAlertOnEditCall is called it needs to either return an empty array or an array with some items in it

When openAlertModalInstance is called it needs to either return undefined or something that passes through to showEquipmentAlertsBasedOnCompanyDetails

showEquipmentAlertsBasedOnCompanyDetails should actually be called, I'll test that method separate, just that it was called

I have manged to write code to test simple functions but nothing like this one so any help will be much appreciated, I spent most of this afternoon trying to figure it out

Cœur
  • 37,241
  • 25
  • 195
  • 267
cjp666
  • 69
  • 2
  • 14

3 Answers3

3

You can use jasmine to mock the function calls that you are not interested in testing. For example, you can tell jasmine to return an empty array every time 'populateCallAlertOnEditCall' is called. I will write an example that might give you an insight:

describe('My Test Spec', function() {
   var myController;

   ...

   beforeEach( inject(($controller) => {
        myController = $controller("myControllerName");
   }));

  it('Testing showCallAlerts when populateCallAlertOnEditCall returns an empty array', inject(function($controller) {
        //setup
        //this will replace every call to populateCallAlertOnEditCall with
        //the function inside callFake
        spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
              return []; //returning an empty array.
        });

        //action
        myController.showCallAlerts(...);

        //assert
        //Do your checking here.
  }));

  it('Testing showCallAlerts when populateCallAlertOnEditCall returns a non-empty array', inject(function($controller) {
        //setup
        //this will replace every call to populateCallAlertOnEditCall with
        //the function inside callFake
        spyOn(myController, 'populateCallAlertOnEditCall ').and.callFake(function() {
              return [1,2,3,4]; //returning a non-empty array.
        });

        //action
        myController.showCallAlerts(...);

        //assert
        //Do your checking here.
  }));

 });
Pedro Vaz
  • 820
  • 5
  • 11
1

the test that something has been called, you can use a Spy

your assertion would look like:

spyOn(obj, 'populateCallAlertOnEditCall')
expect(obj.method).toHaveBeenCalled()

UPDATED:

populateCallAlertOnEditCall = {}
spyOn(obj, 'populateCallAlertOnEditCall.result')
expect(obj.method).toHaveBeenCalled()
brianyang
  • 1,090
  • 13
  • 14
  • Thanks. The bit I can't get my head round is the callModalInstance.result.then, I don't want the real modal instance, I want to create a fake one but then have it continue with the code – cjp666 Sep 09 '16 at 07:18
  • 1
    You probably want to spy on the `result` method of `callModalInstance` spy and mock are not the same. more details on this post: http://stackoverflow.com/questions/24413184/can-someone-explain-the-difference-between-mock-stub-and-spy-in-spock-framewor create an empty object, and `spy` on the result method: – brianyang Sep 10 '16 at 02:07
1

The kind of behaviour you want is called mocking

In Jasmine, mocking is done with Spy Objects, you can read more about those here

Basically, you can use mocks to test if functions were called with the expected parameters.

var xhr = mock( XMLHttpRequest );

xhr.send();

expect( xhr.send ).toHaveBeenCalled();
Community
  • 1
  • 1
Fábio Junqueira
  • 2,701
  • 21
  • 20