小编dee*_*eps的帖子

如何在角度单位测试中模拟警报

我试图模拟我的应用程序中使用的警报

这是我的工厂代码

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

我尝试将此警报的模拟测试编写为

 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" ');
});
Run Code Online (Sandbox Code Playgroud)

});

但是当我尝试运行茉莉花测试时,我收到此错误

Expected spy unknown to have been called with [ 'Alert Message (A): …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing angularjs

3
推荐指数
1
解决办法
3494
查看次数

标签 统计

angularjs ×1

javascript ×1

unit-testing ×1