Test return value of a spy

33,998

Solution 1

First, your factory is not returning anything:

factory.calling = function(){
    return $window.location.href = 'tel:' + factory.call;
};

Calls to $window.location.href that are an assignment will not return anything. Seems to me that that call should not be there and your method should be:

factory.calling = function(){
    return 'tel:' + factory.call;
};

Doing it this way, you have a return value. Also, you are not storing the value that is returned anywhere that you can test it. In general, you use a spy to check to see if the method was called, like this:

it('calling method', function(){
    spyOn(factory, 'calling');
    factory.calling();
    expect(factory.calling).toHaveBeenCalled();
});

To check what the method returns, you can call it within the expect block like this:

it('calling method', function(){
    expect(factory.calling()).toEqual('tel: ' + factory.call);
});

Or you can call it via an anonymous function like this:

it('calling method', function(){
    expect(function(){return factory.calling();}).toEqual('tel: ' + factory.call);
});

Or you can call it first and then check the value it returns like this:

it('calling method', function(){
    var result = factory.calling();
    expect(result).toEqual('tel: ' + factory.call);
});

I might also suggest that you test with fixed data that you provide since a test of this type should not be hitting a database. Also, you should always stick to OAPT (like you have done.)

Solution 2

Not sure you still need this, but call info contains "returnValue" property. So it can be tested like this: expect($scope.isFilterError.calls.first().returnValue).toBeFalsy();

Share:
33,998
Aaron
Author by

Aaron

Updated on July 09, 2022

Comments

  • Aaron
    Aaron almost 2 years

    I am developing an Ionic app. The calling function lets the user call the number in factory.call.

    factory.call = '345-678-9087';
    
    factory.calling = function(){
        return $window.location.href = 'tel:' + factory.call;
    };
    

    This is the Jasmine test for the above,

    it('calling method', function(){
        spyOn(factory, 'calling');
        factory.calling();
        expect(typeof(windowMock.location.href)).toEqual('string');
    });
    

    The above test passes just fine, but it does not test the exact value that factory.calling() returns. I have tried the following with no luck.

    1)

    it('calling method', function(){
        var emergency = spyOn(factory, 'calling');
        factory.calling();
        expect(emergency).toEqual("'tel:' + factory.call");
    });
    

    2) spyOn(factory, "calling").andCallThrough().

    3) spyOn(factory, "calling").andReturn("'tel:' + factory.call").

  • Aaron
    Aaron about 8 years
    When I check the return value of the function factory.calling, it gives me an error - Expected undefined to equal 'tel: 345-678-9087'.
  • MBielski
    MBielski about 8 years
    Sorry, I didn't look at your factory close enough. You should look at it. It doesn't return anything. You are calling a method that does not return anything ($window.location.href) and returning that return value (therefore, undefined). Seems like that call to $window.location.href should not be there, but I'm not familiar with the app.
  • Aaron
    Aaron about 8 years
    I tried it the way you suggested, it gives me the same error - Expected undefined to equal 'tel: 345-678-9087'.
  • MBielski
    MBielski about 8 years
    Update your code in the question, please, so that we can see the changes.