Rap*_*ael 24 unit-testing angularjs angularjs-controller karma-jasmine
当我尝试对我的控制器进行单元测试时,我收到了错误.当我调试测试用例时,我希望得到预期的值,但是它失败了以下错误.我在这里丢失的是什么,或者我是否在错误中测试控制器变量方式?
我的spec文件如下
'use strict';
describe('Information.controller', function () {
beforeEach(angular.mock.module('asp'));
var InformationController, scope;
beforeEach(inject(function($controller, $rootScope) {
scope = $rootScope.$new();
InformationController = $controller('InformationController', {
$scope: scope
});
}));
fit('Should remove the object without info from the object', function () {
InformationController.Data = [
{
"ban": "03745645455",
"accountNo": "0000drgyt456456565",
"id": "2c4cc5e8-f360367"
},
{
"ban": "27346fgh9080",
"accountNo": "000456456ytr655680",
"id": "2c4cc8ae-50bbf360367"
}
];
InformationController.banList = InformationController.Data.map((value, index) => (value && value.ban ? {'id': index, 'text': value.ban} : null))
.filter(value => value);
expect(InformationController.banList.length).toBe(3);
});
});
Run Code Online (Sandbox Code Playgroud)
Rap*_*ael 31
readonly错误是由scope.page(在我的InformationController中)未定义引起的,然后代码试图将属性分配给undefined.Hence修改了beforeEach块,如下所示
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
scope.page3 = {};
InformationController = $controller('InformationController', {
$scope: scope
});
}));
Run Code Online (Sandbox Code Playgroud)
这解决了这个问题.
| 归档时间: |
|
| 查看次数: |
35769 次 |
| 最近记录: |