18

I try to test the code below:

describe('myService test', function () {
    describe('when I call myService.one', function () {
        beforeEach(angular.module('TargetMarketServices'));
        it('returns 1', inject(function (imagesRepository) {
            expect(true).toEqual(true);
        }));

    });

});

When this code is executed I get this error:

TypeError: 'undefined' is not a function (evaluating 'this.func.apply(this.spec)')
    at http://localhost:8080/testacular.js:76
    at http://localhost:8080/context.html:35
ReferenceError: Can't find variable: inject
    at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:6
    at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:8
    at /home/peter/Dropbox/AngularJS/set-component/test/sets/sets-ihm.js:10

PhantomJS 1.8: Executed 1 of 3 (1 FAILED) (skipped 2) (0.072 secs / 0.01 secs)

For my test, I use Testacular with Jasmine and PhantomJS.

2 Answers 2

28

AngularJS provides two testing libraries:

  • angular-mocks.js
  • angular-scenario.js

angular-mocks is used for Jasmine and Karma testing. It publishes global methods module() and inject() to be used in your Jasmine spec tests. This means you must load the angular-mocks.js script (after you load the angular.js library/script)

angular-scenario is only used for e2e testing.

Sign up to request clarification or add additional context in comments.

Comments

13

The line where you have

beforeEach(angular.module('TargetMarketServices'));

should be

beforeEach(module('TargetMarketServices'));

If you take a look at the angular-phonecat project in test/unit/directivesSpec.js it uses

beforeEach(module('myApp.directives'));

If I modify it to use angular.module instead:

beforeEach(angular.module('myApp.directives'));

then I get this error when running testacular also:

TypeError: 'undefined' is not a function (evaluating 'this.func.apply(this.spec)')

5 Comments

But, when I use only module, I get the error "ReferenceError: Can't find variable: module"
Sorry, I meant angular-seed earlier, not angular-phonecat. You are getting that ReferenceError, I'm assuming, because you do not have angular-mocks.js in your testacular unit testing configuration file.
The problem has been resolved by taking the angular-mocks.js file.
How about the answer get updated and accepted? I had the same issue, and this helped me resolve it.
You can also call angular.mock.module('myApp'); instead of module('myApp') if module is overwritten (ie. browserify)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.