【问题标题】:TypeError: jasmine.getEnv().currentSpec is null类型错误:jasmine.getEnv().currentSpec 为空
【发布时间】:2012-06-13 19:25:34
【问题描述】:

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

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)

【问题讨论】:

  • 我遇到了同样的问题,我在测试中错误地写了describe 而不是it
  • 我不认为是这样,因为我在本地机器上运行它,它似乎工作正常。我在隧道 ssh 会话上运行它。也许这与它有关,但它不应该。但确实如此。
  • @rinat.io 谢谢!这就是我的问题!
  • @Satya 你的问题最终解决了吗?您会考虑将其添加为答案吗?
  • 没有解决。我不再尝试像那样远程运行 jasmine。

标签: javascript ruby-on-rails jasmine typeerror


【解决方案1】:

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

感谢rinat-io 的创意。

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 我不认为我在使用角度。
    • 我使用 karma, jasmine 进行 Angular 应用测试。听从了你的建议,但仍然有同样的问题:jasmine.getEnv().currentSpec is null.
    【解决方案3】:

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

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

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

    【讨论】:

    • 没有间谍。
    • 为了帮助人们充分回答这个问题,应该发布完整的示例规范。可能有其他功能正在尝试设置失败的this.currentSpec。我遇到了同样的错误,这就是解决方案,希望它可以帮助其他人。
    【解决方案4】:

    查看这条推文,它有两个修复可能对您有所帮助(它们与您正在使用的 getEnv() 方法相关:

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

    还有 github 链接:

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

    也许其中的一个可以阐明您的问题。

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2014-01-10
        • 1970-01-01
        • 2015-03-06
        • 2013-07-20
        • 2020-06-21
        • 1970-01-01
        • 2013-11-29
        • 2013-03-07
        相关资源
        最近更新 更多