Jak wygląda twój pełny test? Otrzymałem również ten błąd. Mój test wyglądał następująco:
'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);
});
});
Zauważ, że funkcja SpyOn znajduje się w bloku opisu. Patrząc na kod jaśminowym stwierdzamy, że SpyOn powinny być w beforeEach
lub it
bloku:
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);
}
};
Są to miejsca, gdzie currentSpec
jest ustawiony. W przeciwnym razie będzie to wartość pusta. Więc w moim przykładzie powinno być:
'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);
});
});
I wtedy to będzie działać, ponieważ spyOn jest w bloku go. Mam nadzieję, że to pomoże.
Miałem ten sam problem, kiedy omyłkowo napisałem "opisz" zamiast "go" w moich testach. –
Nie sądzę, że to było to, ponieważ uruchomiłem to na moim lokalnym pudełku i wydawało się, że działa dobrze. Uruchomiłem to na tunedowanej sesji ssh. Może to miało coś z tym wspólnego, ale nie powinno. Ale tak się stało. – Satya
@ rinat.io Dzięki! To był problem dla mnie! –