0

I am running jasmine unit test against a method which is outside angular controller.

Running the jasmine throws the following error,

TypeError: Cannot read property 'get' of undefined at testMethod

Can anyone help me with fixing the above error. Thanks !

Test.js

function testMethod() {
    var $http = angular.element('html').injector().get('$http');
    $http.get('/testAPI/testCall').success(...);
}

TestSpec.js

it('makes a successful Test call', angular.mock.inject(function ($http, $window) {
    testMethod();
}));

JasmineSpecRunner.html

<html>
<head>
<meta charset="utf-8">
<link rel="shortcut icon" type="image/png" href="lib/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine.css">

<script src="lib/jquery-2.1.3.js"></script>

<script src="lib/jasmine.js"></script>
<script src="lib/jasmine-html.js"></script>
<script src="lib/boot.js"></script>
<script src="lib/angular.js"></script>
<script src="lib/angular-mocks.js"></script>

<!-- include source files here-->
<script src="src/Test.js"></script>

<!-- include spec files here-->
<script src="specs/TestSpec.js.js"></script>
</head>
<body>
<div ng-app="SpecRunner" ng-controller="SpecController"></div>
</body>
</html>

PS: My implementation is based on this stackoverflow answer.

2 Answers 2

0

Try to inject $http in the testMethod function.. like this testMethod($http).

Also, I think you have spelling mistake in this line:

<script src="specs/TestSpec.js.js"></script>

it should be like this:

<script src="specs/TestSpec.js"></script>
Sign up to request clarification or add additional context in comments.

Comments

0

Try using httpBackend service of angular mocks for testing ajax calls to server.

Comments

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.