我试图测试以下服务,似乎无法匹配模拟响应:
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;
});
}
这是Jasmine代码:
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')
有谁能告诉我为什么我会收到此错误?谢谢你的帮助。
答案 0 :(得分:2)
createResolvedPromise
设置getCustomerDetails
以使用您传入的参数值返回一个承诺 - 在本例中为'12345678'
。这意味着customerResponse
的值为'12345678'
。因此,customer
从customerResponse.customers
获取值的customers
未定义,因为字符串上没有属性customer.displayCustomerNumber
。因此,当您尝试评估customer
时会发生错误,因为CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.repeatDuration = 1000;
animation.duration = 30;
animation.additive = YES;
animation.removedOnCompletion = NO;
animation.fillMode = kCAFillModeBoth;
animation.fromValue = [NSNumber numberWithFloat:0];
animation.toValue = [NSNumber numberWithFloat:360];
[self.SearchButton.layer addAnimation:animation forKey:@"rotationAnimation"];
[CATransaction begin]; {
[CATransaction setCompletionBlock:^{
[UIView animateWithDuration:0.6 delay:2 options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.SearchButton setFrame:CGRectMake(self.slideMenuButton.frame.origin.x, self.slideMenuButton.frame.origin.y, self.SearchButton.frame.size.width, self.SearchButton.frame.size.height)];
[UIView animateWithDuration:0.6 delay:10.f options:UIViewAnimationOptionCurveEaseIn animations:^{
[self.navigationController presentViewController:Search animated:YES completion:nil];
}completion:^(BOOL finished) {
[self.SearchButton setFrame:CGRectMake(searchOrigin.x, searchOrigin.y, self.SearchButton.frame.size.width, self.SearchButton.frame.size.height)];
}];
} completion:^(BOOL finished) {
[self.SearchButton.layer removeAnimationForKey:@"rotationAnimation"];
}];
}];
}
未定义。