Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'undefined' is not a function when using spyOn with Karma

I've been using Yeoman to setup projects, and was trying to build an angular module project that would apply feature toggles. I'm getting hung up with Jasmine testing at the unit level, here is one of my tests:

var scope, Rules;
var elm;
// load the controller's module
beforeEach(function() {
  module('featureToggle.directives', function($provide) {
    $provide.decorator('Rules', function($delegate) {
      $delegate.resolveByName = jasmine.createSpy();

      return $delegate;
    });
  });

  inject(function(_Rules_) {
    Rules = _Rules_;
  });
});

beforeEach(inject(function($rootScope, $compile) {
  scope = $rootScope.$new();
  elm = angular.element(
    '<h1 feature-toggle feature-name="hello">' +
    'Hello World' +
    '</h1>');

  scope = $rootScope;
  $compile(elm)(scope);
  scope.$digest();
}));

it('should render the content when the feature is enabled', function() {
  jasmine.spyOn(Rules, 'resolveByName').andReturn(false);

  var contents = elm.find('h1');
  expect(contents.eq(0)).toHaveClass('hidden');
});

I'm getting an odd failure.

TypeError: 'undefined' is not a function (evaluating 'jasmine.spyOn(Rules, 'resolveByName')')

The karma.conf is very vanilla.

frameworks = ['jasmine'];

// list of files / patterns to load in the browser
files = [
   JASMINE,
   JASMINE_ADAPTER,
   'app/components/angular/angular.js',
   'app/components/angular-mocks/angular-mocks.js',
   'src/*.js',
   'src/**/*.js',
   'test/mock/**/*.js',
   'test/spec/**/*.js'
];

I'm using PhantomJS by and large, but the same issue exists (with a different dialect of the same error) in Chrome.

Using $log, nothing is actually undefined. So I'm rather lost.

like image 943
Ryan Norris Avatar asked Aug 14 '26 14:08

Ryan Norris


1 Answers

Change jasmine.spyOn() to just spyOn().

Btw, not sure why you are spying the same method twice.

like image 96
Vojta Avatar answered Aug 16 '26 14:08

Vojta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!