Kiểm tra đầy đủ của bạn trông như thế nào? Tôi cũng nhận được lỗi này. Bài kiểm tra của tôi trông giống như sau:
'use strict';
describe("CalendarController", function() {
var scope, $location, $controller, createController;
var baseTime = new Date(2014, 9, 14);
spyOn(Date.prototype, 'getMonth').andReturn(baseTime.getMonth());
spyOn(Date.prototype, 'getDate').andReturn(baseTime.getDate());
spyOn(Date.prototype, 'getFullYear').andReturn(baseTime.getFullYear());
var expectedMonth = fixture.load("months.json")[0];
beforeEach(module('calendar'));
beforeEach(inject(function ($injector) {
scope = $injector.get('$rootScope').$new();
$controller = $injector.get('$controller');
createController = function() {
return $controller('CalendarController', {
'$scope': scope
});
};
}));
it('should load the current month with days', function(){
var controller = createController();
expect(scope.month).toBe(expectedMonth);
});
});
Lưu ý rằng chức năng SpyOn nằm trong khối mô tả. Khi nhìn vào mã nhài chúng tôi thấy rằng SpyOn phải ở trong một beforeEach
hoặc it
khối:
jasmine.Env.prototype.it = function(description, func) {
var spec = new jasmine.Spec(this, this.currentSuite, description);
this.currentSuite.add(spec);
this.currentSpec = spec;
if (func) {
spec.runs(func);
}
return spec;
};
...
jasmine.Env.prototype.beforeEach = function(beforeEachFunction) {
if (this.currentSuite) {
this.currentSuite.beforeEach(beforeEachFunction);
} else {
this.currentRunner_.beforeEach(beforeEachFunction);
}
};
Đây là những nơi mà các currentSpec
được thiết lập. Nếu không, điều này sẽ là null. Vì vậy, trong ví dụ của tôi nó nên là:
'use strict';
describe("CalendarController", function() {
var scope, $location, $controller, createController;
var baseTime = new Date(2014, 9, 14);
var expectedMonth = fixture.load("months.json")[0];
beforeEach(module('calendar'));
beforeEach(inject(function ($injector) {
scope = $injector.get('$rootScope').$new();
$controller = $injector.get('$controller');
createController = function() {
return $controller('CalendarController', {
'$scope': scope
});
};
}));
it('should load the current month with days', function(){
spyOn(Date.prototype, 'getMonth').andReturn(baseTime.getMonth());
spyOn(Date.prototype, 'getDate').andReturn(baseTime.getDate());
spyOn(Date.prototype, 'getFullYear').andReturn(baseTime.getFullYear());
var controller = createController();
expect(scope.month).toBe(expectedMonth);
});
});
Và sau đó điều này sẽ làm việc vì spyOn là trong đó khối. Hy vọng điều này sẽ hữu ích.
Tôi đã có cùng một vấn đề, khi tôi nhầm lẫn đã viết 'mô tả' thay vì' nó' trong các bài kiểm tra của tôi. –
Tôi không nghĩ đó là nó, khi tôi chạy nó trên hộp địa phương của tôi và nó dường như làm việc tốt. Tôi đã chạy nó trên một phiên ssh tunnelled. Có lẽ điều đó liên quan đến nó, nhưng nó không nên có. Nhưng nó đã làm. – Satya
@ rinat.io Cảm ơn! Đó là vấn đề đối với tôi! –