Angular2 httpbackend模拟REST请求

时间:2017-06-09 14:59:34

标签: rest angular mocking backend httpbackend

我试图在angular2中运行一个后端模拟REST请求的简单示例。 几天后面临很多问题,得到了" httpbackend"对象我找到了这些指南。

https://github.com/kbaltrinic/http-backend-proxy/issues/1 https://docs.angularjs.org/api/ngMockE2E

安装说明

npm install httpbackend --save-dev
npm install angular-mocks --save-dev

规范文件

var HttpBackend = require('httpbackend');

browser.addMockModule('httpbackend', function() {
  angular.module('httpbackend', ['ngMockE2E']);
});

describe('HTTP backend REST methods', function() {
    var httpBackend;
    var result;

    beforeEach(function() {
        httpBackend = new HttpBackend(browser);
    });

    afterEach(function() {
        httpBackend.clear();
    });

    it('should validate action when POST request', function() {
        browser.get('');  

        httpBackend.whenPOST('app/mock-data/post-action.json').respond(function(method, url, data) {
            return [200, data];
        });

        var buttons = [
            'button_A', 
            'button_B', 
            'button_C', 
            'button_9', 
            'button_5', 
            'button_7', 
            'button_6', 
            'base-keyboard-ok' 
        ];

        buttonsClick = function(j){
            var buttonKey = browser.driver.findElement(by.id(buttons[j]));
            buttonKey.click();
        }

        for(j=0; j<buttons.length; j++){
            buttonsClick(j);
        }

        browser.sleep(2000);

        result = element(by.binding('app/mock-data/post-action.json')); 
        expect(result.getText()).toEqual('ABC9576'); //That's the target i want to reach
    });

});

任何帮助都将不胜感激。

0 个答案:

没有答案