最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: jasmine.getEnv().currentSpec is null - Stack Overflow

programmeradmin3浏览0评论

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)

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)

Share Improve this question edited Aug 4, 2015 at 11:58 Mousey 1,86520 silver badges35 bronze badges asked Jun 10, 2012 at 17:38 SatyaSatya 4,47824 silver badges30 bronze badges 5
  • 7 I had same issue, when i mistakenly wrote describe instead of it in my tests. – rinat.io Commented Mar 21, 2013 at 18:35
  • I don't think that was it, as I ran it on my local box and it seemed to work fine. I was running this on a tunnelled ssh session. Maybe that had something to do with it, but it shouldn't have. But it did. – Satya Commented Mar 28, 2013 at 13:53
  • @rinat.io Thanks! That was the problem for me! – Andresch Serj Commented May 27, 2015 at 7:53
  • @Satya Did you solve your Problem in the end? Would you consider adding it as an answer? – Andresch Serj Commented May 27, 2015 at 7:54
  • It wasn't solved. I stopped trying to run jasmine remotely like that. – Satya Commented May 27, 2015 at 15:11
Add a ment  | 

5 Answers 5

Reset to default 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.

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).

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.

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./dfkaye/statuses/423913741374074880

And the github links:

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

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

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./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.

发布评论

评论列表(0)

  1. 暂无评论