gpt4 book ai didi

javascript - karma Jasmine : How to test ionicModal?

转载 作者:行者123 更新时间:2023-11-30 16:31:13 24 4
gpt4 key购买 nike

情况:

在我的 Ionic 应用程序中,我正在测试模式的正确打开。

我已经做了几次尝试,但我收到以下错误:

TypeError: Cannot read property 'then' of undefined

函数:

$scope.open_register_modal = function() 
{
$ionicModal.fromTemplateUrl('templates/project_register.html', {
scope: $scope
}).then(function(modal) {
$scope.modal_register = modal;
$scope.modal_register.show();
});
};

测试:

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();
});
});

});

尝试:

这些是初始化 $ionicModal 的一些尝试:

1.

    $ionicModal = 
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then : function(modal){}
};

2.

    $ionicModal = 
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then: jasmine.createSpy('$ionicModal.then')
};

3.

    $ionicModal = 
{
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl'),
then: jasmine.createSpy('$ionicModal.fromTemplateUrl.then')
};

4.

    $ionicModal = jasmine.createSpyObj('$ionicModal', ['show', 'close','fromTemplateUrl']);

但是他们都报同样的错误:

TypeError: Cannot read property 'then' of undefined

问题:

如何在测试中传递 .then 方法?

如何正确测试 ionicModal?

最佳答案

我对 ionic 一无所知,但我认为你的错误是期望方法 then 是它的一部分。代码

$ionicModal.fromTemplateUrl('templates/project_register.html', {
scope: $scope
}).then(function(modal) {
$scope.modal_register = modal;
$scope.modal_register.show();
});

可以重构为:

var temp=$ionicModal.fromTemplateUrl(
'templates/project_register.html',
{scope: $scope});

temp.then(function(modal) {
$scope.modal_register = modal;
$scope.modal_register.show();
});

所以 then 方法是调用 fromTemplateUrl 返回的对象的一部分

解决方案可能是这样的:

function fakeTemplate() {
return { then:function(){}}
}
$ionicModal = {
fromTemplateUrl: jasmine.createSpy('$ionicModal.fromTemplateUrl').and.callFake(fakeTemplate)
};

关于javascript - karma Jasmine : How to test ionicModal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33296778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com