Ошибка типа: customer.loadProfileCustomer не является функцией

Я пытаюсь заставить этот модульный тест работать, но я застрял в этой части, потому что, возможно, я ошибаюсь или что-то, я очень новичок в модульном тесте с AngularJS и Karma, так что если кто-то может мне помочь с этим Буду очень благодарна.

Ошибка, которая привлекла мое внимание: TypeError: customer.loadProfileCustomer is not a function

customer.loadProfileCustomer из независимой службы, где я могу получить основную информацию о клиенте. Вот дорожка стека:

  HeadlessChrome 0.0.0 (Mac OS X 10.12.6) addressForm component should load customer profile FAILED
            TypeError: customer.loadProfileCustomer is not a function
                at loadCustomerProfile (app/Resources/assets/linio/js/shopping/address/components/addressForm.js:9:3375)
                at AddressFormController.onInit [as $onInit] (app/Resources/assets/linio/js/shopping/address/components/addressForm.js:9:2327)
                at Context.<anonymous> (app/Resources/assets/linio/js/test/specs/shopping/address/components/addressForm.spec.js:250:15)

Внешний сервис: customer.js

(function () {
  'use strict';

  angular
    .module('shopping.customer')
    .factory('customer', customer);

  customer.$inject = ['$http', '$q', 'logger', 'messenger'];
  function customer($http, $q, logger, messenger) {
    var customer = {
      profile: {},
      loadProfileCustomer: loadProfileCustomer,
    };

    function loadProfileCustomer() {
      return $http
        .get('/api/customer/profile', { cache: true })
        .then(onRequestSuccess)
        .catch(onRequestFailure);

      function onRequestSuccess(response) {
        customer.profile = response.data;

        return response.data;
      }
    }
  }
})();

addressForm.js где я показываю информацию о клиенте в форме:

(function () {
  'use strict';

  angular
    .module('shopping.address')
    .component('addressForm', {
      controller: AddressFormController,
      controllerAs: 'addressForm',
      bindings: {
        input: '<',
        edit: '@',
        type: '@',
        hideSaveButton: '@',
      },
      templateUrl: '/ng/address-form',
    });

  AddressFormController.$inject = ['event', 'customer', 'resolveLocation'];
  function AddressFormController(event, customer, resolveLocation) {
    var viewModel = this;
    viewModel.$onInit = onInit;
    viewModel.input = viewModel.input || {};
    viewModel.profileCustomer = {};
    viewModel.loadCustomerProfile = loadCustomerProfile;
    viewModel.hasAddresses = hasAddresses;

    function onInit() {
      if (!hasAddresses()) {
        loadCustomerProfile();
      }
    }

    function loadCustomerProfile() {
      return customer.loadProfileCustomer().then(function (profile) {
        viewModel.input.firstName = profile.firstName;
        viewModel.input.lastName = profile.lastName;
        viewModel.input.phoneNumber = profile.phoneNumber;
      });
    }

    function hasAddresses() {
      return ((customer.addresses).length > 0);
    }
  }
})();

Вот мой юнит тест:

describe('addressForm component', function () {
  var component;
  var scope;
  var customer;

  beforeEach(function () {
    bard.appModule('shopping.address');
    bard.inject('$rootScope', '$componentController', '$q', 'resolveLocation', 'customer', 'event');

    customer = {
      profile: {
        firstName: 'John',
        lastName: 'Smith',
        phoneNumber: '55551234',
      },
    };

    scope = $rootScope.$new();

    component = $componentController('addressForm', { $scope: scope, customer: customer });
  });


  it('should load customer profile', function () {
    component.$onInit();
    component.loadCustomerProfile();
    customer.loadProfileCustomer();
    sinon.stub(customer, 'loadProfileCustomer').returns($q.when({ firstName: 'John', lastName: 'Smith', phoneNumber: '55551234' }));
    // component.loadProfileCustomer().then(function (customer) {
    //   expect(customer).to.exist;
    //   // expect(component.input.firstName).to.be.equal(customer.profile.firstName);
    //   // expect(component.input.lastName).to.be.equal(customer.profile.lastName);
    //   // expect(component.input.phoneNumber).to.be.equal(customer.profile.phoneNumber);
    //   // expect(component.input.firstName).to.be.equal(customer.profile.firstName);
    //   // expect(component.input.lastName).to.be.equal(customer.profile.lastName);
    //   // expect(component.input.phoneNumber).to.be.equal(customer.profile.phoneNumber);
    //   // expect(component.input.firstName).to.be.equal('John');
    //   // expect(component.input.lastName).to.be.equal('Smith');
    //   // expect(component.input.phoneNumber).to.be.equal('55551234');
    //   assertCustomerProfileList(customer);
    // });

    //sinon.stub(customer, 'loadProfileCustomer').returns($q.when(stubs.loadProfileCustomer()));
    $rootScope.$apply();

    // expect(customer.profile.firstName).to.exist;
    // expect(customer.profile.lastName).to.exist;
    // expect(customer.profile.phoneNumber).to.exist;
    expect(component.input.firstName).to.be.equal('John');
    expect(component.input.lastName).to.be.equal('Smith');
    expect(component.input.phoneNumber).to.be.equal('55551234');
    expect(customer).to.exist;
    // expect(component.input.firstName).to.be.equal(customer.profile.firstName);
    // expect(component.input.lastName).to.be.equal(customer.profile.lastName);
    // expect(component.input.phoneNumber).to.be.equal(customer.profile.phoneNumber);

  });

});

1 ответ

Попробуйте загрузить клиентский модуль в вашем модульном тесте. angular.module('shopping.customer')

Другие вопросы по тегам