单元测试中找不到模块

时间:2013-09-20 09:44:06

标签: unit-testing angularjs jasmine angular-strap

添加angularStrap模块后,一切正常,但在我的unittest中我得到以下错误:

    Error: No module: $strap.directives
...
    TypeError: Unable to get property 'expect' of undefined or null reference

所以var httpBackend = null

我将它添加到主模块:

var app = angular.module('testK', ["ngResource", "$strap.directives", "ui.if", 'ui.date']);

将js文件添加到index.html

<!-- AngularJS files -->
        <script type="text/javascript" src="lib/jquery.js"></script>
        <script type="text/javascript" src="lib/jquery-ui-1.10.3.custom.js"></script>
        <!-- <script type="text/javascript" src="lib/jquery-ui-1.10.3.custom_witheffects.js"></script> -->
        <script type="text/javascript" src="lib/angular/angular.js"></script>
        <script type="text/javascript" src="lib/angular/angular-resource.js"></script>
        <!--<script type="text/javascript" src="lib/angular/directives/ng-table.js"></script>-->
        <script type="text/javascript" src="lib/angular/ui/if.js"></script>
        <script type="text/javascript" src="lib/angular/ui/datepicker.js"></script>
        <script type="text/javascript" src="lib/angular/twitter-bootstrap.min.js"></script>
        <script type="text/javascript" src="lib/angular/angular-strap-0.7.5.min.js"></script>

我的unittest:

"use strict";
describe("mainT", function () {


 var ctrl, scope, httpBackend, locationMock, 

    beforeEach(module("testK"));
    beforeEach(inject(function ($controller, $rootScope, $httpBackend, $location, $injector) {
        scope = $rootScope.$new();
        httpBackend = $httpBackend;
        locationMock = $location;



        ctrl = $controller("mainT.controller.users", { $scope: scope, $location: locationMock});


    }));

    afterEach(function () {
        httpBackend.verifyNoOutstandingRequest();
        httpBackend.verifyNoOutstandingExpectation();
    });




        describe("method test", function () {
        it('should test', function () {
            //todo implementation
            expect(true).toBeFalsy();
        });
    });
});

当我从我的app模块中删除“$ strap.directives”时,我的unittest工作正常。有人知道为什么在我的unittest中找不到这个带模块?

1 个答案:

答案 0 :(得分:2)

karma.conf.js文件定义了单元测试期间可用的文件。 它可以用来添加模拟或第三方模块。

检查karma.conf.js并在那里添加文件:

// base path, that will be used to resolve files and exclude
basePath = '';

// list of files / patterns to load in the browser
files = [
  JASMINE,
  JASMINE_ADAPTER,
  REQUIRE,
  REQUIRE_ADAPTER,
  'test-main.js',
  ...
  ];