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
Add a ment  | 

3 Answers 3

Reset to default 9

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.

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>

本文标签: