vendredi 1 janvier 2016

how to mock a alert in unit testing of angular

I am trying to mock the alert used in my application

Here is my factory code

 app.factory('Handler', ['config', '$location',function (config, $location){
    return {
        log: function (message, data) {
            if (config.DEBUG) {
                alert('Alert Message (' + message + "):\n" + JSON.stringify(data));
            }
        }
    }
   }]);

I tried to write the mock test for this alert as

 describe("checking  Handler service " , function(){
  var Handler, mock ;
  beforeEach(module("MyApp"));
  beforeEach(function(){
  mock = {alert: jasmine.createSpy()};
  inject(function(_Handler_){
    Handler = _Handler_;
    });
});
it("should check for alert",function(){
    spyOn(Handler, "log");
    Handler.log('A','B');
    expect(mock.alert).toHaveBeenCalledWith('Alert Message (A):\n "B" ');
});

});

But I get this error whenver I try running jasmine test

Expected spy unknown to have been called with [ 'Alert Message (A): "B" ' ] but it was never called.

Aucun commentaire:

Enregistrer un commentaire