【发布时间】:2017-01-09 19:23:01
【问题描述】:
我已经使用 Angular 1.5 组件创建了导航。但面临测试困难。我是 Angular 和单元测试的新手。 请在PLUNKER上找到代码
这是我的组件。
module.component('firstComponent', {
templateUrl: "1.html",
bindings: {
"$router": "<"
},
controller: function($rootScope) {
var $ctrl = this;
$rootScope.title = "Title from Page 1";
$ctrl.goToNextPage = function() {
$ctrl.$router.navigate(["Second"]);
};
}
});
我正在尝试测试我的当前页面是否有正确的标题以及它是否正在导航到下一页。
这是我的 test-spec.js
describe("Check if component is defined", function() {
beforeEach(module("app"));
var $ctrl;
var router;
var $rootscope;
beforeEach(inject(function($componentController) {
$ctrl = $componentController("firstComponent", {
$router: router,
$rootscope: $rootscope
});
}));
it("are things define properly", function() {
expect($ctrl).toBeDefined();
expect($ctrl.goToNextPage).toBeDefined();
});
it("should have proper title", function() {
expect($rootscope.title).toBe("Title from Page 1");
});
it("should navigate to next page", function() {
expect($ctrl.router.navigate).toHaveBeenCalled();
});
});
这些是运行测试时遇到的错误:
3 种规格,2 次失败 1. TypeError: Cannot read property 'title' of undefined 2. TypeError: Cannot read property 'navigate' of undefined
【问题讨论】:
标签: javascript angularjs unit-testing testing jasmine