如何使用玩笑来配置我的angular.module('app')的全局参数?

时间:2018-12-19 08:17:10

标签: angularjs jestjs angular-test

我的angular模块是一个全局参数,以这种样式定义控制器或服务很容易

(function(App) {
    App.controller('HelloWorldController', helloWorldController)
})(App)

但是如何使用Jest模拟全局参数“ App”? 我的测试代码(helloworld.controller.spec.js):

require('angular');
require('angular-mocks');
require('./helloworld.controller');

describe('helloworldController', function() {
    var $controller;
    var $rootScope;

    beforeEach(inject(function(_$controller_, _$rootScope_){
        $controller = _$controller_;
        $rootScope = _$rootScope_;
    }))

    describe('helloworldController.getText', function() {
        it('should link two string when execute getText function', function() {
            var $scope = $rootScope.$new();
            var controller = $("HelloWorldController", {$scope: $scope});
            var stringA = 'hello';
            var stringB = 'world';
            expect('hello, world').toEqual(controller.getText(stringA, stringB));
        })
    })
})

开玩笑的时候,我得到了这个错误

 FAIL  src/component/helloworld/helloworld.controller.spec.js
  ● Test suite failed to run

    ReferenceError: App is not defined

      30 |     */
      31 |     App.controller('HelloWorldController', helloWorldController)
    > 32 | })(App)
         |    ^

      at Object.App (src/component/helloworld/helloworld.controller.js:32:4)
      at Object.require (src/component/helloworld/helloworld.controller.spec.js:4:1)

Test Suites: 1 failed, 1 total

我应该如何解决这个问题?

0 个答案:

没有答案