[英]Unit testing with routing in angular
Just started learning angular. 刚开始学习角度。 Because i like its unit testing power, i decided to try Karma/Jasmine unit testing on it. 因为我喜欢它的单元测试功能,所以我决定尝试对其进行Karma / Jasmine单元测试。
this was my first test app: 这是我的第一个测试应用程序:
angular.module('notesApp')
.controller('ListCtrl', [function() {
var self = this;
self.items = [
{id: 1, label: 'First', done: true},
{id: 2, label: 'Second', done: false}
];
self.getDoneClass = function(item) {
return {
finished: item.done,
unfinished: !item.done
};
}; }]);
this was my unit test app: 这是我的单元测试应用程序:
describe('Controller: ListCtrl', function() {
beforeEach(module('notesApp'));
var ctrl;
beforeEach(inject(function($controller) { ctrl = $controller('ListCtrl'); }));
it('should have items available on load', function() {
expect(ctrl.items).toEqual([
{id: 1, label: 'First', done: true},
{id: 2, label: 'Second', done: false}
]);
});
it('should have highlight items based on state', function() {
var item = {id: 1, label: 'First', done: true};
var actualClass = ctrl.getDoneClass(item);
expect(actualClass.finished).toBeTruthy();
expect(actualClass.unfinished).toBeFalsy();
item.done = false;
actualClass = ctrl.getDoneClass(item);
expect(actualClass.finished).toBeFalsy();
expect(actualClass.unfinished).toBeTruthy();
});
});
And it worked fine. 而且效果很好。 But once i tried to test my application which had routing configuration (application was working in browser, i just wanted to test it in unit tests), it started throwing injection errors once tests started. 但是,一旦我尝试测试具有路由配置的应用程序(应用程序正在浏览器中运行,我只想在单元测试中对其进行测试),一旦测试开始,它就会开始引发注入错误。 So i copied this first application that was working and just added config with routing to it and it started to fail tests again. 因此,我复制了第一个正在运行的应用程序,并刚刚添加了带有路由功能的配置,它开始再次失败测试。
angular.module('notesApp', ['ngRoute'])
app.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when("/", {
templateUrl: "/Templates/index-partial.html"
}).when("/details", {
templateUrl: "/Templates/details-partial.html"
}).when("/create", {
templateUrl: "/Templates/create-partial.html"
}).when("/edit", {
templateUrl: "/Templates/edit-partial.html"
}).when("/delete", {
templateUrl: "/Templates/delete-partial.html"
}).otherwise({
templateUrl: "/Templates/index-partial.html"
});
// use the HTML5 History API
$locationProvider.html5Mode({
enabled: true,
requireBase: false
});
})
.controller('ListCtrl', [function() {
var self = this;
self.items = [
{id: 1, label: 'First', done: true},
{id: 2, label: 'Second', done: false}
];
self.getDoneClass = function(item) {
return {
finished: item.done,
unfinished: !item.done
};
}; }]);
I did include all dependencies in karma config file. 我确实在业力配置文件中包括了所有依赖项。 Actually I included every angular script just to make sure. 实际上,我只是为了确保包含每个角度脚本。
'../Scripts/angular.js',
'../Scripts/angular-route.js',
'../Scripts/angular-sanitize.js',
'../Scripts/angular-cookies.js',
'../Scripts/ui-bootstrap.js',
'../Scripts/ui-bootstrap-tpls.js',
'../Scripts/angular-mocks.js',
'../Scripts/angular-resource.js',
'../Modules/myApp.js',
'../Modules/myApp_UT.js'
Now i don't know what Im doing wrong and i would love some guidelines because im stuck :| 现在我不知道我在做什么错,我会喜欢一些指导,因为我被困住了:|
One of Errors that is shown in karma debug page : [$injector:unpr] Unknown provider: $scopeProvider <- $scope karma调试页面中显示的错误之一:[$ injector:unpr]未知提供程序:$ scopeProvider <-$ scope
Link to error: https://docs.angularjs.org/error/$injector/unpr?p0=$scopeProvider%20%3C-%20$scope%20%3C-%20ReadListCtrl 链接到错误: https : //docs.angularjs.org/error/$injector/unpr?p0=$scopeProvider%20%3C-%20$scope%20%3C-%20ReadListCtrl
There are multiple other erros but I think most of them are there because controller couldn't be created properly. 还有许多其他错误,但我认为其中大多数都存在,因为无法正确创建控制器。
before your actual app injection you need to inject ngRoute ie 在实际应用注入之前,您需要注入ngRoute即
beforeEach(module('ngRoute'));
beforeEach(module('notesApp'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.