简体   繁体   English

业力茉莉花:测试模式的结束

[英]Karma-Jasmine: Testing the closing of a modal

THE SITUATION: 情况:

I am testing the proper working of a modal in my Angular / Ionic app. 我正在Angular / Ionic应用程序中测试模态的正常工作。

So far I can properly test the fact that the modal has been called, but I don't know how to test the proper closing of the modal. 到目前为止,我可以正确地测试已调用模式的事实,但我不知道如何测试模式的正确关闭。

Have made several attempts and read similar questions, but didn't find a solution. 进行了几次尝试并阅读了类似的问题,但没有找到解决方案。

THE CODE: 编码:

The controller: 控制器:

Code working fine in the app, it has just been refactored to facilitate the unit test. 代码在应用程序中运行良好,它刚刚进行了重构以方便单元测试。

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

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

$scope.close_login_modal = function() 
{
    $scope.modal_login.hide();
};

The test: 考试:

The first test (open modal) is working fine and passes. 第一个测试(开放模式)工作正常并通过。 The second test I don't know how to do it. 第二项测试我不知道该怎么做。

describe('App tests', function() 
{
    beforeEach(module('my_app.controllers'));

    // mocking the .then method of $ionicModal.fromTemplateUrl (it works)
    function fakeTemplate() 
    {
        return { 
            then: function(modal){
                $scope.modal_login = modal;
            }
        }
    }

    beforeEach(inject(function(_$controller_, _$rootScope_)
    {
        $controller = _$controller_;
        $rootScope = _$rootScope_;
        $scope = _$rootScope_.$new();

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

        var controller = $controller('MainCtrl', { $scope: $scope, $rootScope: $rootScope, $ionicModal: $ionicModal });
    }));


    describe('Modal tests', function() 
    {
        it('should open login modal', function() 
        {
            $scope.open_login_modal();

            expect($ionicModal.fromTemplateUrl).toHaveBeenCalled();
            expect($ionicModal.fromTemplateUrl.calls.count()).toBe(1);
        });

        it('should close login modal', function() 
        {
            $scope.close_login_modal();
            // How can i test here that the modal has been close?
        });
    });

});

THE ERROR: 错误:

This is the error message: TypeError: Cannot read property 'hide' of undefined 这是错误消息: TypeError: Cannot read property 'hide' of undefined

THE QUESTION: 问题:

How can I test the close of the modal? 如何测试模态的关闭? How can I be sure that the function hide() has been properly called? 如何确定函数hide()已被正确调用?

It has to do with the declaring of the modal in the test? 它与测试中模态的声明有关吗?

Thank you very much! 非常感谢你!

EDIT: 编辑:

This answer properly replied to the question 'how to test the closing of a modal' giving the right solution to include before the opening of the modal. 该答案正确回答了“如何测试模态的关闭”问题,给出了在模态打开之前包括的正确解决方案。 If you want to know how to properly spy a modal i have asked in a separate question: 如果您想知道如何正确监视模式,请在另一个问题中询问:

Karma-Jasmine: How to properly spy on a Modal? 业力茉莉花:如何正确监视一个模态?

The given answer give also the rule how to spy a premise in general. 给出的答案还给出了一般如何监视前提的规则。

Your close test requires the modal login to have been opened and since this is not happening you are getting that error: 您的关闭测试需要打开模式登录,由于这种情况没有发生,因此您将收到该错误:

I would rewrite you test something like: 我会重写您测试类似的东西:

describe('Modal tests', function() {
  beforeEach(function(){
    $scope.open_login_modal();
  });
  it('should open login modal', function() {
    expect($ionicModal.fromTemplateUrl).toHaveBeenCalled();
    expect($ionicModal.fromTemplateUrl.calls.count()).toBe(1);
  });
  it('should close login modal', function() {
    $scope.close_login_modal();
    spyOn($scope.modal_login, 'hide');
    expect($scope.modal_login.hide()).toHaveBeenCalled();
  });
});

So both tests require the the open_login_modal function to be called and so I added to a beforeEach. 因此,这两个测试都需要调用open_login_modal函数,因此我将其添加到beforeEach中。 You will also need a spy on the hide function for the modal too. 您还将需要对模式的hide函数进行监视。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Karma-Jasmine:如何正确窥探模态? - Karma-Jasmine: How to properly spy on a Modal? Karma-jasmine 我如何在模态中测试关闭的 function - Karma-jasmine How i test a close function in a modal 使用业力茉莉花测试规范内的对象属性更改 - Testing an objects property change within a spec with karma-jasmine 使用EJS指数进行Angma Karma-Jasmine单元测试 - Angular Karma-Jasmine Unit Testing with EJS index 单击链接,Angular 2和业力茉莉花单元测试用例打开模态 - Angular 2 and karma-jasmine unit test case to open modal on click of link 单元测试:如果不隐式调用$ rootScope。$ digest(),则Karma-Jasmine无法解决Promise。 - Unit Testing: Karma-Jasmine not resolving Promise without implicitly calling $rootScope.$digest(); Foundation-Apps角度站点与karma-jasmine单元测试集成 - Foundation-Apps angular site integration with karma-jasmine unit testing 如何使用 karma-jasmine 在 function 中使用具有异步等待的单击处理程序自动测试 function? - How to automate testing function with click handler that have async await inside the function using karma-jasmine? 如何使用带有karma-jasmine适配器的testr? - How to use testr with karma-jasmine adapter? 离子2业力茉莉花单元测试 - Ionic 2 karma-jasmine unit tesing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM