首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TypeError: jasmine.getEnv().currentSpec为空

TypeError: jasmine.getEnv().currentSpec为空
EN

Stack Overflow用户
提问于 2012-06-11 01:38:13
回答 3查看 4.4K关注 0票数 18

当我尝试运行我的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 )

EN

回答 3

Stack Overflow用户

发布于 2015-07-11 07:54:11

我也看到了同样的错误。我在describe中直接使用了一条expect语句。我在it块中移动了expect,错误就消失了。

感谢rinat-io的点子。

票数 1
EN

Stack Overflow用户

发布于 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应该在beforeEachit块中:

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块中。希望这能有所帮助。

票数 0
EN

Stack Overflow用户

发布于 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

也许其中一个会让你的问题一目了然。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10970809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档