我得到了一个失败的测试与错误ReferenceError: dmeApp is not defined。我需要在测试中注入dmeApp吗?我正在遵循我在这里发现的一个教程:http://andyshora.com/unit-testing-best-practices-angularjs.html
app.js:
angular.module('dmeApp', ['ngRoute', 'dmeApp.library'])
.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider
.when('/library', {
templateUrl: 'library.html',
controller: 'LibraryController',
})
.when('/styleguide', {
templateUrl: 'styleguide.html',
})
.otherwise({
templateUrl: 'front.html',
});
$locationProvider.html5Mode(true);
}])
.controller('NavController', ['$scope', '$location', function($scope, $location) {
$scope.linkIsActive = function(viewLocation) {
return viewLocation === $location.path();
};
}]);app.spec.js:
describe('Header Navigation', function() {
beforeEach(angular.module('dmeApp'));
it('should have a NavController defined', function() {
expect(dmeApp.NavController).toBeDefined();
});
});以及我的Karma配置(在Gruntfile.js中):
/**
* Our Karma configuration.
*/
karma: {
options: {
files: [
'<%= vendor_files.js %>',
'<%= vendor_files.offline_js %>', // angular is added here
'<%= vendor_files.test_js %>', // angular-mock is added here
'src/**/*.js', // all app files and specs are added here
],
browsers: ['Chrome'],
frameworks: ['jasmine'],
},
dev: {
reporters: 'dots',
background: true,
},
continuous: {
singleRun: true,
},
},发布于 2014-05-08 11:57:57
在spec.js中的beforeEach中,尝试如下所示:
beforeEach(inject(function($injector) {
var dmeAppNavController = $injector.get('dmeApp.NavController');
...
}然后可以在测试用例中使用dmeAppNavController。
在测试用例中使用$injector几乎总是一个好主意。
https://stackoverflow.com/questions/23539630
复制相似问题