admin管理员组

文章数量:1399955

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.

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.
Share Improve this question edited Jan 1, 2016 at 10:28 Codor 17.6k9 gold badges32 silver badges57 bronze badges asked Jan 1, 2016 at 9:28 deepsdeeps 4914 silver badges12 bronze badges 1
  • Don't use alert. Use dedicated log service (create one) with abstract log behaviour. Then expect(logService.log).toHaveBeenCalled(). – dfsq Commented Jan 1, 2016 at 10:49
Add a ment  | 

1 Answer 1

Reset to default 7

You can simply mock the function. I have choosen to use $window instead. I removed the config since I wasn't sure where you were getting this. And I removed the '/n' from the result, since it was messing with the pare. But this is the way to go:

angular.module('MyApp', [])

.factory('Handler', ['$location', '$window', function ($location, $window){
    return {
        log: function (message, data) {
           $window.alert('Alert Message (' + message + "): " + JSON.stringify(data));
        }
    }
 }]);

And the test:

 describe("checking  Handler service " , function(){
    var Handler, _$location_, $window;

    beforeEach(module("MyApp"));

    beforeEach(function($provide) {
       module(function ($provide) {
         $window = { alert: function(message) {

         }};
         spyOn($window, "alert");
         $provide.value('$window', $window);
      });
    });

    beforeEach(inject( function(_Handler_, _$location_) {
       Handler = _Handler_;
       $location = _$location_;

       spyOn(Handler, "log").andCallThrough();

    }));

  it("should check for alert",function(){
      Handler.log('A','B');
      expect(Handler.log).toHaveBeenCalledWith('A', 'B');
      expect($window.alert).toHaveBeenCalledWith( 'Alert Message (A): "B"' );
  });
});

You can see the result in this plunkr.

本文标签: javascripthow to mock an alert in unit testing of angularStack Overflow