2017-10-04 86 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')

相关问题