javascriptangularjsunit-testingkarma-jasmineangularjs-injector

Karma Error: [$injector:modulerr] Failed to instantiate module due to: (Index.html is working perfectly)


So I set up an angular js project in webstorm and everything works properly except for the karma testing suite

I am setting up the model as follows:

beforeEach(module('[modulename]'));

and my tests are:

it('should work', ((function() {
        //spec body
        expect(true).toBe(true);
    })));

works and passes, but

it('should work, does it?', (inject(function(_$http_) {
        //spec body
        $http=_$http_;
        expect(true).toBe(true);
    })));

not only fails the test but suddenly my module is no longer loading :S

Error: [$injector:modulerr] Failed to instantiate [modulename] due to: Error: [$injector:nomod] Module '[modulename]' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

why though even putting in both tests seems to work perfectly fine for the first test (without inject) - the second one still fails like previously stated...

IMPORTANT: everything works if I run my application in a browser window, so the error has to be in the test file or something :S

I'm rather new to angular js and karma so I hope this is not a stupid question.


Solution

  • Try using $httpBackend instead of $http. Also, injection and mocking should be done outside the test cases in the test setup phase. use beforeEach() block for any setup and instantiation code.