【发布时间】:2014-11-20 12:06:38
【问题描述】:
我第一次尝试为我的代码编写单元测试。在我的控制器顶部声明时,我可以测试 $scopes,但是如何测试 $scope 函数内的 $scopes?
Ctrl:
app.controller("MyCtrl", function($scope, $http, myService) {
$scope.initialiseValue = 0;
$scope.address = function (value) {
$scope.addressValue = 0;
}
});
测试:
describe('app', function () {
var app;
beforeEach(function () {
app = angular.mock.module('app')
});
describe('MyCtrl', function () {
var scope, ctrl, theService , httpMock;
beforeEach(inject(function($controller, $rootScope, myService, $httpBackend) {
scope = $rootScope.$new(),
ctrl = $controller('MyCtrl', {
$scope: scope,
$location : location,
myService: theService ,
$httpBackend: httpMock
});
}));
// This test succeeds ///////////
it('initialiseValue should be initialised to 0', function() {
console.log(scope.initialiseValue );
expect(scope.initialiseValue ).toBe(0);
});
// This test fails ///////////
it('addressValue should be initialised to 0', function() {
console.log(scope.addressValue );
expect(scope.addressValue ).toBe(0);
});
});
错误:
Expected undefined to be false.
使用 Karma 和 Jasmine。
任何帮助将不胜感激。
【问题讨论】:
标签: javascript angularjs unit-testing angularjs-scope karma-runner