我正在尝试测试以下服务,但似乎无法匹配模拟响应:

public getCustomerDetails(customerID:string): ng.IPromise<ICustomerDetails> {
    return this.testService.getCustomerDetails(customerID).then((customerResponse:ICustomerResult) => {

        var customer = customerResponse.customers;

        var customerDetailsResult:ICustomerDetails = {
            customerNumber: customer.displayCustomerNumber,
            name: customer.name,
            userId: customer.buId,
            customerType: customer.type,
            address: customer.displayAddress
        };

        return customerDetailsResult;
    });
}

这是茉莉花代码:
describe('CustomerService', () => {
    var mockTestService: any = {};
    var $q: ng.IQService;
    var createCustomerDetailsService;
    var customerDetailsService;
    var createResolvedPromise;
    var resolvePromises;

    var serviceResponse = {
        customerNumber: 'string',
        name: 'name',
        userId: 'buId',
        customerType: 'type',
        address: 'displayAddress'
    };

    var rootScope;

    beforeEach(() => {
        module('app.customer');

        inject(( _$q_, $injector) => {
            this.$q = _$q_;
            rootScope = $injector.get('$rootScope');

            createResolvedPromise = (result) => {
                return this.$q.when(result);
            };

            resolvePromises = () => {
                rootScope.$apply();
            };

            createCustomerDetailsService = () => {
                return new app.customer.CustomerService(
                    mockRnsService);
            };
        });

    });


    it('WILL search by customer ID and return a customers details', () => {
        var searchResponsePromise;

        mockTestService.getCustomerDetails = jasmine.createSpy("getCustomerDetails").and.callFake(createResolvedPromise);
        customerDetailsService = createCustomerDetailsService();
        searchResponsePromise = customerDetailsService.getCustomerDetails('12345678');
        resolvePromises();

        expect(searchResponsePromise).toEqual(serviceResponse);
    });

});

我得到的错误是:
TypeError: 'undefined' is not an object (evaluating 'customer.displayCustomerNumber')
有人能告诉我为什么我会犯这个错误吗?谢谢你的帮助。

最佳答案

createResolvedPromise设置getCustomerDetails以返回带有您传入的参数值的承诺-在本例中为'12345678'。这意味着customerResponse的值是'12345678'。因此,从customer获取其值的customerResponse.customers是未定义的,因为字符串上没有属性customers。因此,当您试图计算customer.displayCustomerNumber时发生错误,因为customer未定义。

09-19 19:44