[英]Karma-Jasmine: How to test ionicModal?
THE SITUATION: 情况:
In my Ionic app I am testing the correct opening of a modal. 在我的Ionic应用程序中,我正在测试模态的正确打开。
I have made several attempts, but i am getting the following error: 我做了几次尝试,但出现以下错误:
TypeError: Cannot read property 'then' of undefined
THE FUNCTION: 功能:
$scope.open_register_modal = function()
{
$ionicModal.fromTemplateUrl('templates/project_register.html', {
scope: $scope
}).then(function(modal) {
$scope.modal_register = modal;
$scope.modal_register.show();
});
};
THE TEST: 考试:
describe('App tests', function() {
beforeEach(module('my_app.controllers'));
beforeEach(inject(function(_$controller_, _$rootScope_)
{
$controller = _$controller_;
$rootScope = _$rootScope_;
$scope = _$rootScope_.$new();
$ionicModal =
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then : function(modal){} // <--- attempt
};
var controller = $controller('MainCtrl', { $scope: $scope, $rootScope: $rootScope, $ionicModal: $ionicModal });
}));
describe('Modal tests', function()
{
it('should open register modal', function()
{
$scope.open_register_modal();
expect($ionicModal).toHaveBeenCalled();
});
});
});
ATTEMPTS: 尝试:
These are some of the attempts to initialize $ionicModal: 这些是初始化$ ionicModal的一些尝试:
1. 1。
$ionicModal =
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then : function(modal){}
};
2. 2。
$ionicModal =
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then: jasmine.createSpy('$ionicModal.then')
};
3. 3。
$ionicModal =
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then: jasmine.createSpy('$ionicModal.fromTemplateUrl.then')
};
4. 4。
$ionicModal = jasmine.createSpyObj('$ionicModal', ['show', 'close','fromTemplateUrl']);
But they all give the same error: 但是它们都给出相同的错误:
TypeError: Cannot read property 'then' of undefined
THE QUESTION: 问题:
How can i pass the .then method inside the test? 如何通过测试中的.then方法?
How can i properly test ionicModal? 我如何正确测试ionicModal?
I don't know anything about ionic, but I think your mistake is expecting that the method then
is part of it. 我不知道什么离子,但我认为你的错误期待,该方法
then
是其中的一部分。 The code 编码
$ionicModal.fromTemplateUrl('templates/project_register.html', {
scope: $scope
}).then(function(modal) {
$scope.modal_register = modal;
$scope.modal_register.show();
});
can be refactor to: 可以重构为:
var temp=$ionicModal.fromTemplateUrl(
'templates/project_register.html',
{scope: $scope});
temp.then(function(modal) {
$scope.modal_register = modal;
$scope.modal_register.show();
});
so the method then
is part of the object returned by the call to fromTemplateUrl
因此该方法
then
是通过调用所返回的对象的一部分fromTemplateUrl
A solution could be something like: 解决方案可能是这样的:
function fakeTemplate() {
return { then:function(){}}
}
$ionicModal = {
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl').and.callFake(fakeTemplate)
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.