TypeError:jasmine.getEnv()。currentSpec为null

当我尝试运行我的茉莉花规格时,我明白了

TypeError: jasmine.getEnv().currentSpec is null in http://localhost:8888/__JASMINE_ROOT__/jasmine.js (line 498) 

不知道为什么,甚至不知道从哪里开始寻找。

498行是:

 return jasmine.getEnv().currentSpec.expect(actual); 

我几个月来一直在做茉莉花,但不是这个项目。 我以前从未见过这种情况。

那么,我从哪里开始呢?

(这是rails 3.x项目中的茉莉花gem)

我看到了同样的错误。 我直接在里面describe了一个expect陈述。 我把expect移到了一个阻止内部,错误就消失了。

感谢rinat-i​​o的想法。

我认为问题在于angular-mocks.jsangular-scenario.js中不同版本的angular如果您的配置如下所示:

 files = [ JASMINE, JASMINE_ADAPTER, '../app/lib/angular/angular.js', // ANGULAR_SCENARIO, // ANGULAR_SCENARIO_ADAPTER, '../app/lib/angular/angular-scenario.js', '../app/lib/angular/jstd-scenario-adapter.js', '../app/lib/angular/jstd-scenario-adapter-config.js', '../app/lib/angular/angular-mocks.js', '../app/lib/angular/angular-resource.js', '../app/lib/angular/angular-cookies.js', '../app/js/**/*.js', '**/*Spec.js' ]; 

尽量避免使用ANGULAR_SCENARIOANGULAR_SCENARIO_ADAPTER – 将其替换为嵌入角度源中的那些(’../app/lib/angular/angular-scenario.js’,’.. / app / lib / angular / jstd-scenario-在我的例子中,adapter.js’,’.. / app / lib / angular / jstd-scenario-adapter-config.js’)。

你的完整测试是什么样的? 我也遇到了这个错误。 我的测试看起来像这样:

 '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或者阻塞:

 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在它的块中。 希望这可以帮助。

查看这条推文,它有两个修复,也许一个可以帮助你(它们与你正在使用的getEnv()方法有关:

https://twitter.com/dfkaye/statuses/423913741374074880

和github链接:

https://github.com/dfkaye/jasmine-intercept https://github.com/dfkaye/jasmine-where

也许其中一个会揭示你的问题。

我遇到了这个问题,发现了以下文章。 好像茉莉花版和Angular版本没有合作。 当我进行更改时,文章概述为angular-mocks.js,错误消失了。

http://railsware.com/blog/2014/09/09/make-angularjs-1-0-7-work-with-jasmine-2-0/

我在参加PluralSight课程时遇到了这个问题: 使用Bootstrap,AngularJS,ASP.NET,EF和Azure构建站点。 在unit testing模块期间。