Karma-Jasmine:如何测试离子模型? [英] Karma-Jasmine: How to test ionicModal?

查看:139
本文介绍了Karma-Jasmine:如何测试离子模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

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

In my Ionic app I am testing the correct opening of a modal.

我已经多次尝试,但是我收到以下错误:

I have made several attempts, but i am getting the following error:

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

});

ATTEMPTS:

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

These are some of the attempts to initialize $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']);

但他们都给出了同样的错误:

But they all give the same error:

TypeError: Cannot read property 'then' of undefined

问题:

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

How can i pass the .then method inside the test?

我如何正确测试ionicModal?

How can i properly test ionicModal?

推荐答案

我对离子没什么了解,但我认为你的错误是期待方法那么就是它的一部分。代码

I don't know anything about ionic, but I think your mistake is expecting that the method then is part of it. The code

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

所以方法然后是调用返回的对象 fromTemplateUrl

so the method then is part of the object returned by the call to fromTemplateUrl

解决方案可能类似于:

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

这篇关于Karma-Jasmine:如何测试离子模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆