jeudi 9 août 2018

Mocking api angular 4

I want to mock some api calls. But I can't get it working.

This is what I want to implement.

myAppDev = angular.module('myAppDev', ['myApp', 'ngMockE2E']);
    myAppDev.run(function($httpBackend) {
      var phones = [{name: 'phone1'}, {name: 'phone2'}];

      // returns the current list of phones
      $httpBackend.whenGET('/phones').respond(phones);

      // adds a new phone to the phones array
      $httpBackend.whenPOST('/phones').respond(function(method, url, data) {
        var phone = angular.fromJson(data);
        phones.push(phone);
        return [200, phone, {}];
      });
      $httpBackend.whenGET(/^\/templates\//).passThrough(); // Requests for templates are handled by the real server
      //...
    });

I want to use the e2e tests for angular. And I have written some tests inside the app.e2e-spec.ts. Wat I thought was to setup the listeners for mockdata inside the beforeEach method. Something like this:

beforeEach(() => {
    page = new AppPage();

     $httpBackend.when('GET', 'mydomain')
         .respond(200,
             {   "id":"211220edc80"}
         );
})

But that isn't working. Does somebody know what to import, and where. And how I can use this?

Aucun commentaire:

Enregistrer un commentaire