TypeError: jasmine.getEnv().currentSpec is null

耗尽温柔 提交于 2020-01-24 02:03:48

问题


When I try to run my jasmine specs, I get

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

No idea why, not even sure where to begin looking.

Line 498 is:

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

I've been doing jasmine for several months now, but not on this project. I've never seen this happening before.

So, where do I start?

(This is jasmine gem in a rails 3.x project)


回答1:


I was seeing the same error. I had an expect statement directly inside describe. I moved the expect inside an it block an the error went away.

Thanks to rinat-io for the idea.




回答2:


I think the problem is in different versions of angular in angular-mocks.js and angular-scenario.js If your config looks like this:

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'
];

try to avoid ANGULAR_SCENARIO and ANGULAR_SCENARIO_ADAPTER - replace it with ones that are embedded into your angular source ('../app/lib/angular/angular-scenario.js', '../app/lib/angular/jstd-scenario-adapter.js', '../app/lib/angular/jstd-scenario-adapter-config.js' in my case).




回答3:


What does your full test look like? I was also getting this error. My test looked like this:

'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);
  });
});

Notice that the SpyOn function is in the describe block. When looking at the jasmine code we find that SpyOn should be in a beforeEach or it block:

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);
  }
};

These are the places where the currentSpec is set. Otherwise this will be null. So in my example it should be:

'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);
  });
});

And then this will work because the spyOn is in the it block. Hope this helps.




回答4:


Check out this tweet, it has two fixes maybe one helps you out (they are related with the getEnv() method you are using:

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

And the github links:

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

Maybe one of those will shed a light on your problem.




回答5:


I was running into this issue and found the following article. It seems as though the jasmine version and Angular versions were not working together. When I made the changes the article outlines to angular-mocks.js the error went away.

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

I ran into this while taking the PluralSight course: Building a Site with Bootstrap, AngularJS, ASP.NET, EF and Azure. During the Unit testing module.



来源:https://stackoverflow.com/questions/10970809/typeerror-jasmine-getenv-currentspec-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!