2016-12-16 2 views
0

私はアプリケーションでいくつかのエンドツーエンドのテストを得るために分度器を使用しています。テストでは、私は次のようMockModuleとバックエンドの呼び出しをあざけり:分度器オーバーライド "beforeAll" httpモック

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を上書きする簡単な方法を達成するための良い方法があるかどうか私は疑問に思ってこれには本当に新しいです。

答えて

2

これを実現するには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'); 
    }); 
}); 
+0

これも試しましたか?モジュール内で使用されているので、私はbeforeEachから$ httpBakendに到達することはできません。これはbeforeEechで新しいcustomerE2Eを作成することで解決できますが、これは醜いと思われ、変数statusCodesとCurrentTestNumberにも到達できません – willedanielsson