admin管理员组文章数量:1313783
I'm trying to get a basic unit test working and am running into an issue using angular-mocks.js. Hopefully this code will explain my situation.
describe("peconfigApp", function () {
beforeEach(function() {
angular.mock.module('peconfigApp');
});
describe("Binaries Controller", function () {
it("should work", function () {
expect(true).toBe(true);
});
});
});
Jasmine result error:
TypeError: Object #<Object> has no method 'module'
at null.<anonymous> (http://localhost:58141/Tests/js/controller-tests.js:4:22)
at jasmine.Block.execute (http://localhost:58141/Tests/lib/jasmine.js:1064:17)
I've placed alerts in different parts of the code to verify that angular-mocks.js is loading before my tests load and run so I am pretty sure loading is not an issue.
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript" src="../lib/angular-mocks.js"></script>
<script type="text/javascript" src="lib/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-html.js"></script>
<script type="text/javascript" src="../js/controllers.js"></script>
<script type="text/javascript" src="js/controller-tests.js"></script>
I'm trying to get a basic unit test working and am running into an issue using angular-mocks.js. Hopefully this code will explain my situation.
describe("peconfigApp", function () {
beforeEach(function() {
angular.mock.module('peconfigApp');
});
describe("Binaries Controller", function () {
it("should work", function () {
expect(true).toBe(true);
});
});
});
Jasmine result error:
TypeError: Object #<Object> has no method 'module'
at null.<anonymous> (http://localhost:58141/Tests/js/controller-tests.js:4:22)
at jasmine.Block.execute (http://localhost:58141/Tests/lib/jasmine.js:1064:17)
I've placed alerts in different parts of the code to verify that angular-mocks.js is loading before my tests load and run so I am pretty sure loading is not an issue.
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript" src="../lib/angular-mocks.js"></script>
<script type="text/javascript" src="lib/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-html.js"></script>
<script type="text/javascript" src="../js/controllers.js"></script>
<script type="text/javascript" src="js/controller-tests.js"></script>
Share
Improve this question
asked Nov 20, 2013 at 23:20
ChrisChris
2,8061 gold badge31 silver badges35 bronze badges
3 Answers
Reset to default 9I moved the angular-mocks load after the jasmine loads.
<script type="text/javascript" src="../lib/angular.min.js"></script>
<script type="text/javascript" src="lib/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-html.js"></script>
<script type="text/javascript" src="../lib/angular-mocks.js"></script>
<script type="text/javascript" src="../js/controllers.js"></script>
<script type="text/javascript" src="js/controller-tests.js"></script>
This makes it work. Dumb problem, I must have missed this somewhere while reading through docs and tutorials.
In order to setup the module
in your unit test you usually would do something like this:
beforeEach(module('peconfigApp'));
As long as your config is set up correctly (to pull in the correct files), that should allow you to then address the module in the test.
The documentation on unit testing is pretty good and worth a couple reads.
Hope this helps.
I just spent an hour trying to get jasmine to read my controllers, etc. Everything has to be in the exact right order as Chris says.
my index.html for running jasmine:
<html ng-app="myApp">
<body>
<link href="jasmine.css" rel="stylesheet" />
<!-- Init Angular -->
<script src="../angular.min.js"></script>
<script src="../angular-resource.min.js"></script>
<!-- My code -->
<script src="../app/controller.js"></script>
<script src="../app/main.js"></script>
<script src="../app/service.js"></script>
<!-- Init Jasmine -->
<script src="../jasmine.js"></script>
<script src="../jasmine-html.js"></script>
<script src="../angular-mocks.js"></script>
<!-- Insert spec's-->
<script src="spec/controller.spec.js"></script>
<!-- And GO! -->
<script src="runner.js"></script>
<!-- A basic test to check "connectivity" -->
<div id="container" ng-controller="myCtrl">
<h1>Say something... {{data.variablehere}}</h1>
</div>
<div id="HTMLReporter" class="jasmine_reporter"></div>
</body>
</html>
本文标签:
版权声明:本文标题:javascript - AngularJS testing with Jasmine unable to call "angular.mock.module" while using angular-mocks.js 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741883589a2402892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论