我是编写测试单元的新手,在尝试执行测试时收到以下错误:
TypeError: undefined is not an object (evaluating '$scope.createRoleForm.canCreateAccount=false')
我想测试的功能是:
function enableAccountRoles() {
$scope.createRoleForm.canCreateAccount = false;
$scope.createRoleForm.canListAccounts = false;
$scope.createRoleForm.canSearchAllChat = false;
$scope.createRoleForm.canManageSite = false;
}
我的测试代码是
it('should call enableAccountRoles', function() {
this.scope.enableAccountRoles();
})
我知道这还没有完成,因为我没有期望 - 在运行测试单元之前,我是否需要在测试文件中设置变量的值?
答案 0 :(得分:0)
是的,您需要设置预期值。请参阅下面的示例。
Javascript函数测试
function Add(x, y) {
return x + y;
}
TestCase代码:
describe("Math Test Cases", function () {
it("Additon Test Case", function () {
expect(Add(3, 4)).toBe(7);
});
});
角度测试代码:
var MyApp = angular.module("MyModule", []);
MyApp.controller("MyController", function ($scope) {
$scope.number1 = 0;
$scope.number2 = 0;
$scope.total = 0;
$scope.Addition = function () {
$scope.total = $scope.number1 + $scope.number2;
};
});
Angular TestCase:
describe("Angular Test cases", function () {
it("Addition test case", function () {
module("MyModule");
var scope = {};
inject(function ($controller) {
$controller("MyController", { $scope: scope });
});
scope.number1 = 4;
scope.number2 = 5;
scope.Addition();
expect(scope.total).toBe(9);
});
});