茉莉花说变量未定义,当它在函数中定义时
Jasmine saying variable is undefined, when it gets defined in a function
这是我的 sp.js
:
angular.module("SpPageApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
self.add = function() {
BaseService.add.sp(self.sp, function() {
self.cerrorMessages = BaseService.cerrorMessages;
});
};
}]);
这是我的 test_sp.js
:
describe('Controller: MainCtrl', function() {
var ctrl, mockBaseService;
beforeEach(function() {
mockBaseService = {
spls: 'x',
cerrorMessages: 'y',
add: { sp: function(something, cb) { cb() } },
fetch: { selfsps: function(cb) { cb() } },
logout: function() {},
};
module('BaseApp', function($provide) {
$provide.value('BaseService', mockBaseService);
});
module('SpPageApp');
inject(function($controller) {
ctrl = $controller('MainCtrl', {
});
});
});
it('add() calls through to BaseService.add.sp.', function() {
ctrl.sp = "{'spName': 'test post'}"
spyOn(mockBaseService.add, 'sp');
ctrl.add();
expect(mockBaseService.add.sp).toHaveBeenCalledWith(ctrl.sp, jasmine.any(Function));
expect(ctrl.cerrorMessages).toBe(mockBaseService.cerrorMessages);
});
});
当我通过 karma start
测试代码时,它引发了这个错误:
Expected undefined to be 'y'.
at Object.<anonymous> (/home/a/Documents/CMS/CMSApp/static/js/karma/tests/test_sp.js:53:38)
并指向行:
expect(ctrl.cerrorMessages).toBe(mockBaseService.cerrorMessages);
怎么说 ctrl.cerrorMessages
是未定义的,尽管在 ctrl.add()
执行后,它在 BaseService.add.sp()
的回调中被定义如下:
self.cerrorMessages = BaseService.cerrorMessages;
?我尝试在控制器的开头初始化变量,如下所示:
self.cerrorMessages;
但这也不起作用(出现了同样的错误)。
您可能想调用 through 以便调用实际方法
spyOn(mockBaseService.add, 'sp').and.callThrough();
这是我的 sp.js
:
angular.module("SpPageApp", ["BaseApp"])
.controller("MainCtrl", ["$http", "$window", "BaseService", function($http, $window, BaseService) {
var self = this;
self.add = function() {
BaseService.add.sp(self.sp, function() {
self.cerrorMessages = BaseService.cerrorMessages;
});
};
}]);
这是我的 test_sp.js
:
describe('Controller: MainCtrl', function() {
var ctrl, mockBaseService;
beforeEach(function() {
mockBaseService = {
spls: 'x',
cerrorMessages: 'y',
add: { sp: function(something, cb) { cb() } },
fetch: { selfsps: function(cb) { cb() } },
logout: function() {},
};
module('BaseApp', function($provide) {
$provide.value('BaseService', mockBaseService);
});
module('SpPageApp');
inject(function($controller) {
ctrl = $controller('MainCtrl', {
});
});
});
it('add() calls through to BaseService.add.sp.', function() {
ctrl.sp = "{'spName': 'test post'}"
spyOn(mockBaseService.add, 'sp');
ctrl.add();
expect(mockBaseService.add.sp).toHaveBeenCalledWith(ctrl.sp, jasmine.any(Function));
expect(ctrl.cerrorMessages).toBe(mockBaseService.cerrorMessages);
});
});
当我通过 karma start
测试代码时,它引发了这个错误:
Expected undefined to be 'y'.
at Object.<anonymous> (/home/a/Documents/CMS/CMSApp/static/js/karma/tests/test_sp.js:53:38)
并指向行:
expect(ctrl.cerrorMessages).toBe(mockBaseService.cerrorMessages);
怎么说 ctrl.cerrorMessages
是未定义的,尽管在 ctrl.add()
执行后,它在 BaseService.add.sp()
的回调中被定义如下:
self.cerrorMessages = BaseService.cerrorMessages;
?我尝试在控制器的开头初始化变量,如下所示:
self.cerrorMessages;
但这也不起作用(出现了同样的错误)。
您可能想调用 through 以便调用实际方法
spyOn(mockBaseService.add, 'sp').and.callThrough();