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 abstractlog
behaviour. Thenexpect(logService.log).toHaveBeenCalled()
. – dfsq Commented Jan 1, 2016 at 10:49
1 Answer
Reset to default 7You 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
版权声明:本文标题:javascript - how to mock an alert in unit testing of angular - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744215193a2595605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论