18

当我尝试运行我的茉莉花规格时,我得到

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 项目中的 jasmine gem)

4

5 回答 5

1

我看到了同样的错误。我有一个expect直接在里面的声明describe。我将expect内部移动了一个it块,错误消失了。

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

于 2015-07-10T23:54:10.520 回答
0

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

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

和github链接:

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

也许其中之一会阐明您的问题。

于 2015-03-02T16:00:25.547 回答
0

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

'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 函数位于描述块中。查看 jasmine 代码时,我们发现 SpyOn 应该位于 abeforeEachit块中:

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。否则这将为空。所以在我的例子中应该是:

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

于 2014-10-14T20:36:23.517 回答
0

我认为问题出在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')。

于 2013-05-14T15:28:22.210 回答
0

我遇到了这个问题并找到了以下文章。好像 jasmine 版本和 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 构建站点。 在单元测试模块期间。

于 2015-06-05T21:37:53.453 回答