【发布时间】:2017-08-11 08:12:29
【问题描述】:
我在函数内部调用了一个 $mdDialog。我想对 $mdDialog ok 进行单元测试并取消案例。
以下是我的控制器代码 (app.controller.js)。
(function () {
'use strict';
app.controller('AppCtrl', AppCtrl);
AppCtrl.$inject = ['$scope', '$mdDialog'];
function AppCtrl($scope, $mdDialog) {
$scope.saveEntry = function (ev) {
var confirm = $mdDialog.prompt()
.title('Save Entry')
.textContent('If you want, you can add a description to explain what you changed.')
.placeholder('Version Description')
.ariaLabel('Version Description')
.initialValue('')
.targetEvent(ev)
.ok('Save')
.cancel('Cancel');
$mdDialog.show(confirm).then(function (result) {
$scope.status = true;
}, function () {
$scope.status = false;
});
};
}
})();
以下是规范代码(app.controller.spec.js):
describe('Unit test AppController: mdDialog', function () {
var $controller, $mdDialog;
beforeEach(function () {
module('App');
inject(function (_$controller_, _$mdDialog_) {
$controller = _$controller_;
$mdDialog = _$mdDialog_;
});
});
it(': Opened', function () {
var $scope = {};
var controller = $controller('AppCtrl', { $scope: $scope });
var $mdDialogOpened = false;
$mdDialog.show = jasmine.createSpy().and.callFake(function () {
$mdDialogOpened = true;
});
$scope.saveEntry();
$scope.$digest();
expect($mdDialog.show).toHaveBeenCalled;
expect($mdDialogOpened).toBe.true;
});
});
当我运行上述代码时,我收到以下错误: TypeError: Cannot read property 'then' of undefined
我提到了这个 GitHub 问题 https://github.com/angular/material/issues/1482。但我的问题没有得到解决方案
提前致谢
【问题讨论】:
标签: angularjs unit-testing angular-material karma-jasmine angular-mock