AngularJS testing with Jasmine unable to call “angular.mock.module” while using angular-mocks.js

北慕城南 提交于 2019-12-23 18:35:29

问题


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>

回答1:


I 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.




回答2:


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.




回答3:


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>


来源:https://stackoverflow.com/questions/20109235/angularjs-testing-with-jasmine-unable-to-call-angular-mock-module-while-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!