当我尝试运行我的jasmine规范时,我得到
TypeError: jasmine.getEnv().currentSpec is null in
http://localhost:8888/__JASMINE_ROOT__/jasmine.js (line 498)
不知道为什么,甚至不确定从哪里开始寻找。
第498行是:
return jasmine.getEnv().currentSpec.expect(actual);
我已经做了几个月的jasmine了,但不是在这个项目上。我以前从来没有见过这样的事情。
那么,我该从哪里开始呢?
(这是rails 3.x项目中的jasmine gem )
发布于 2015-07-11 07:54:11
我也看到了同样的错误。我在describe
中直接使用了一条expect
语句。我在it
块中移动了expect
,错误就消失了。
感谢rinat-io的点子。
发布于 2014-10-15 04:36:24
你的完整测试是什么样子的?我也得到了这个错误。我的测试是这样的:
'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);
});
});
注意,SpyOn函数位于describe块中。在查看jasmine代码时,我们发现SpyOn应该在beforeEach
或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);
}
};
这些是设置currentSpec
的地方。否则,它将为null。因此,在我的示例中,应该是:
'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);
});
});
然后,这将会起作用,因为spyOn在it块中。希望这能有所帮助。
发布于 2015-03-03 00:00:26
查看这条推文,它有两个修复,其中一个可能会帮助您(它们与您正在使用的getEnv()方法相关:
https://twitter.com/dfkaye/statuses/423913741374074880
和github链接:
https://github.com/dfkaye/jasmine-intercept https://github.com/dfkaye/jasmine-where
也许其中一个会让你的问题一目了然。
https://stackoverflow.com/questions/10970809
复制相似问题