我有一个使用Angular Translate(
https://github.com/PascalPrecht/angular-translate)的应用程序.翻译通过浏览器在应用程序中运行良好,但当我尝试测试任何控制器时,我得到错误:意外请求:GET locale / locale-en.json.我如何对我的控制器进行单元测试,因为translate在启动时会对语言文件进行GET请求?
我正在使用与Karma的yeoman角度发生器.
App.js:
angular.module('myApp',['ngCookies','ui.bootstrap','pascalprecht.translate']) .config(function ($routeProvider,$locationProvider,$translateProvider) { $routeProvider .when('/',{ templateUrl: 'views/main.html',controller: 'MainCtrl' }) .otherwise({ redirectTo: '/' }); $translateProvider.useStaticFilesLoader({ prefix: 'locale/locale-',suffix: '.json' }); $translateProvider.uses('en'); $translateProvider.useLocalStorage(); });
控制器测试:
describe('Controller: DocumentationCtrl',function () { // load the controller's module beforeEach(module('myApp')); var DocumentationCtrl,scope,$httpBackend; // Initialize the controller and a mock scope beforeEach(inject(function ($controller,$rootScope,$injector) { $httpBackend = $injector.get('$httpBackend'); scope = $rootScope.$new(); DocumentationCtrl = $controller('DocumentationCtrl',{ $scope: scope }); })); it('should attach a list of awesomeThings to the scope',function () { $httpBackend.whenGET('locale/locale-en.json').respond(200,{ "TITLE": 'My App' }); expect(scope.awesomeThings.length).toBe(3); }); });
文档控制器只是标准生成的控制器.