$ httpBackend没有存根第二个函数的响应

时间:2016-11-29 20:03:28

标签: javascript angularjs unit-testing httpbackend

我是角度单元测试的新手,我在这个角度应用程序中使用Mocha / Chai进行单元测试。我能够存根getApplications()函数的响应,但我无法获得load()函数的响应。两者都使用$ http来发出GET请求,唯一的区别是load()正在为网址中的参数使用id。

当我运行测试时,WidgetsCreateController.widget = { fields: [], type: 'content而不是我的模拟小部件{ name: 'test' }

需要注意的一点是,我有一个函数reset()在控制器的构造函数中运行,它调用getApplicationsload ---不确定,如果这也导致单元测试的请求问题?

这是我的控制器:

class WidgetsCreateController {
  constructor($http, $uibModal, $state, $stateParams, basicModal) {
   this.$http = $http;
   this.$uibModal = $uibModal;
   this.$state = $state;
   this.$stateParams = $stateParams;
   this.basicModal = basicModal;

 // editing or creating?
   this.editing = (this.$state.current.name === 'widgets_edit');
   this.reset();
 }

  reset() {
   this.widget = {
     fields: [],
     type: 'content'
  };
  this.load();
  this.getApplications();
 }

  getApplications() {
    var self = this;
    this.applications = [];
    this.$http.get('/api/applications').then(response => {
      self.applications = response.data.map((app) => {
        return {
         label: app.name,
         value: app._id
       };
     });
     this.setupFields();
   }).catch(response => {
     console.error(response);
   });
  }

 load() {
  var self = this;
  var id = this.$stateParams.id;
  if(!this.editing) {
    return;
  }
  this.$http.get('/api/widgets/' + id).then(response => {
    self.widget = response.data;
  }).catch(response => {
    self.basicModal.error(
      'Uh-oh.',
      'Something went wrong while loading this widget - details are below.',
       response
    ).finally(() => {
      self.$state.go('widgets_list');
    });
  });
}

这是我的单元测试:

 'use strict';

describe('Controller: WidgetsCreateController', function() {

 var WidgetsCreateController, $httpBackend, $q, scope, $http, $state, $stateParams, $uibModal, basicModal;

// load the controller's module
beforeEach(module('testApp'));

// Initialize the controller and a mock scope
   beforeEach(inject(function($rootScope, $controller, _$httpBackend_, _$q_, _$http_, _$state_, _$stateParams_, _$uibModal_, _basicModal_) {
   scope = $rootScope.$new();
   $httpBackend = _$httpBackend_;
   $http = _$http_;
   $q = _$q_;
   $state = _$state_;
   $stateParams = _$stateParams_;
   $uibModal = _$uibModal_;
   basicModal = _basicModal_;
   WidgetsCreateController = $controller('WidgetsCreateController', {$scope:     scope, $http, $stateParams, basicModal});
 }));

 describe('When calling the reset function', function() {
  var myLoadFunction,
  myGetApplicationsFunction;

  beforeEach(function() {
    myLoadFunction = sinon.spy(WidgetsCreateController, 'load');
    myGetApplicationsFunction = sinon.spy(WidgetsCreateController,     'getApplications');
    WidgetsCreateController.reset();
  });

  it('should create a widget object', function() {
    var widget = WidgetsCreateController.widget;

    expect(widget).to.have.property('fields')
      .that.is.an('array');

    expect(widget).to.have.property('type')
     .that.is.a('string')
     .that.equals('content');
 });

  it('should call the load function at least once', function() {
    expect(myLoadFunction).to.have.been.called;
  });

  it('should call the getApplications function at least once', function() {
    expect(myGetApplicationsFunction).to.have.been.called;
  });

 });

describe('When calling the getApplications function', function() {
  var apps = [{name: 'test', _id: 1}];
  var radioApps = [{label: 'test', value: 1}];

  beforeEach(function() {
    $httpBackend.expect('GET', '/api/applications').respond(200, []);
    $httpBackend.expectGET('app/auth/routes/login/auth.login.html');
  });

it('should load mocked applications', function() {

  $httpBackend
    .when('GET', '/api/applications')
    .respond(200, apps);

  $httpBackend.flush();

  expect(WidgetsCreateController.applications).to.be.an('array');
  expect(WidgetsCreateController.applications).to.be.ok;
  expect(WidgetsCreateController.applications).to.not.be.an('undefined');
  expect(WidgetsCreateController.applications).to.not.be.empty;
  expect(WidgetsCreateController.applications).to.eql(radioApps);

 });
});

  describe('When calling the load function', function() {
   var widget = { name: 'test' };
   var id;

   beforeEach(function() {
    /// Test had an unexpected request from /api/applications, which is why I added it here
    $httpBackend.expect('GET', '/api/applications').respond(200, []);
    id = WidgetsCreateController.$stateParams.id = 1;
  });

   it('should make a GET to widgets/:id', function() {
     WidgetsCreateController.load();

     $httpBackend
      .when('GET', '/api/widgets/' + id)
      .respond(200, widget);

     $httpBackend.flush();

     expect(WidgetsCreateController.widget).to.be.an('object')
      .that.equals(widget);
    });
 });

0 个答案:

没有答案