2017-10-04 36 views
0

我試圖讓這個單元測試工作,但我被困在這部分,因爲也許我錯了什麼的,我很新的與單元測試與AngularJS和因果報應,所以,如果任何人都可以幫我這個,我將非常感激。類型錯誤:customer.loadProfileCustomer不是一個函數

,這引起了我的注意的錯誤是: 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); 

    }); 

}); 

回答

0

嘗試加載客戶模塊單元測試。 angular.module('shopping.customer')

相關問題