我在尝试测试方法控制器时遇到了一些问题。所以,这是我的控制器,名为contactCtrl
'use strict';
(function () {
angular.module('routerApp').controller('ContactController', function ($scope, contactRepository) {
$scope.saveContact = function(selectedContact) {
$scope.errors = [];
contactRepository.saveContactInfo(selectedContact);
$scope.contactSelected = false;
};
$scope.cancel = function() {
$scope.contactSelected = false;
$scope.selectedContact = null;
};
$scope.selectContact = function(contact) {
$scope.contactSelected = true;
$scope.selectedContact = contact;
};
});
}());
这是我的联系人存储库
'use strict';
(function () {
angular.module('routerApp').factory('contactRepository', function() {
return {
saveContactInfo: function (selectedContact) {
console.log(selectedContact);
}
};
});
}());
这是我的名为 contactCtrl.spec.js 的规范文件
describe('Controller',function() {
var scope,contactCtrl;
beforeEach(module('routerApp'));
beforeEach(inject(function ($rootScope, $controller) {
scope = $rootScope.$new();
contactCtrl = $controller('ContactController',{$scope:scope});
}));
describe('ContactController', function() {
it('save method should have contactSelected false', function() {
expect(contactCtrl.contactSelected).toBe(false);
});
});
})
我想测试当我运行 save 方法时是否真的有效。