¿Cómo es tu prueba completa? También estaba recibiendo este error. Mi prueba se veía así:
'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);
});
});
Observe que la función SpyOn está en el bloque describir. Al mirar el código de jazmín nos encontramos con que SpyOn debe estar en un bloque beforeEach
o it
:
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);
}
};
Estos son los lugares donde se desarrolla la currentSpec
. De lo contrario, será nulo. Así que en mi ejemplo que debe ser:
'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);
});
});
Y entonces esto va a funcionar porque el SpyOn está en el bloque de la misma. Espero que esto ayude.
Tuve el mismo problema, cuando por error escribí 'describe' en lugar de' it' en mis pruebas. –
No creo que fuera eso, ya que lo ejecuté en mi caja local y parecía funcionar bien. Estaba ejecutando esto en una sesión ssh tunelizada. Tal vez eso tuvo algo que ver con eso, pero no debería haberlo hecho. Pero lo hizo. – Satya
@ rinat.io Gracias! Ese fue el problema para mí! –