【问题标题】:Protractor override "beforeAll" http mock量角器覆盖“beforeAll”http模拟
【发布时间】:2016-12-16 13:01:46
【问题描述】:

我使用 Protractor 是为了在我的应用程序中进行一些端到端测试。在测试中,我使用 MockModule 模拟后端调用,如下所示:

describe('Lets test a feature' , function(){
    beforeAll(function () {
        var customerE2E = function () {
            angular.module('customerE2E', ['customer', 'ngMockE2E'])
                .run(function ($httpBackend) {

                    $httpBackend.whenPOST('/api/data').respond(200);
                    $httpBackend.whenGET(/^.*/).passThrough();
            });
        };
        browser.addMockModule('customerE2E', customerE2E);
    });

    it('correctly demonstrates my problem', function () {
        expect(element(by.css('h4')).getText()).toMatch('Hello world');
    }    
})

这确实很好用,但我的问题是,当帖子以 404、500 等响应时,我还想测试我的应用程序。我已经通过为每种情况设置一个新的“描述”功能来解决这个问题,但它会是很高兴能够从“it”函数内部覆盖这个调用。

it('correctly demonstrates my problem', function () {
    expect(element(by.css('h4')).getText()).toMatch('Hello world');
}

it('show error due to bad request', function () {
    /* 
     Replace $httpBackend.whenPOST('/api/data').respond(200);
     with $httpBackend.whenPOST('/api/data').respond(404);
    */
    expect(element(by.css('h4')).getText()).toMatch('Failed api call');
} 

我对此真的很陌生,所以我想知道是否有一种很好的方法来实现一种简单的方法来覆盖在 beforeAll 函数中设置的早期 MockModuled。

【问题讨论】:

    标签: angularjs testing jasmine protractor integration-testing


    【解决方案1】:

    你可以使用beforeEach()来实现。

    describe('Test Mock Module',function () {
    
        var statusCodes = [200,404,500];
        var currentTestNumber = 0;
        beforeAll(function () {
            var customerE2E = function () {
                angular.module('customerE2E', ['customer', 'ngMockE2E'])
                    .run(function ($httpBackend) {
    
                        $httpBackend.whenPOST('/api/data').respond(200);
                        $httpBackend.whenGET(/^.*/).passThrough();
                    });
            };
            browser.addMockModule('customerE2E', customerE2E);
        });
    
        /*Below method will be executed before each test and set the required response respectively*/
        beforeEach(function () {
            $httpBackend.whenPOST('/api/data').respond(statusCodes[currentTestNumber++]);
        });
    
        it('test 200 status code',function () {
            expect(element(by.css('h4')).getText()).toMatch('Message for 200 status code');
        });
    
        it('test 404 status code',function () {
            expect(element(by.css('h4')).getText()).toMatch('Message for 404 status code');
        });
    
        it('test 500 status code',function () {
            expect(element(by.css('h4')).getText()).toMatch('Message for 500 status code');
        });
    });
    

    【讨论】:

    • 你试过这个吗?我无法从 beforeEeach 到达 $httpBakend,因为它在模块中使用。这可以通过在 beforeEech 中创建一个新的 customerE2E 来解决,但这看起来很难看,我也无法访问变量 statusCodes 和 CurrentTestNumber
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-04
    • 2015-06-04
    • 1970-01-01
    • 1970-01-01
    • 2014-06-15
    相关资源
    最近更新 更多