我正在尝试编写一个单元测试来创建一个checklistItem,其属性定义如下:
typedef NS_ENUM (NSUInteger, ChecklistStatus) { Pending, Completed };
@protocol IChecklistItem <NSObject>
@property (nonatomic, assign, readonly) NSInteger Id;
@property (nonatomic, copy, readonly) NSString *Description;
@property (nonatomic, assign, readonly)BOOL IsCompleted;
@property (nonatomic, assign, readwrite) ChecklistStatus Status;
@property (nonatomic, strong, readwrite) NSDate *CompletedDate;
@property (nonatomic, copy, readwrite) NSString *CompletedByUserId;
@property (nonatomic, assign, readonly) NSInteger RoleId;
@property (nonatomic, assign, readonly) NSInteger GroupId;
@property (nonatomic, strong, readonly) NSArray<IChecklistNote> *Notes;
现在我在我的XCTest中的设置方法中进行了此设置:
_service = [[ChecklistService alloc]initWithUrl:[NSURL URLWithString:kcChecklistUrl] credentialsManager:self.credentialsManager];
这是我的单元测试的其余部分:
- (void)testCreateChecklistItem {
XCTAssertNotNil(_service);
CCChecklistItem *checklistItem = [CCChecklistItem new];
CCChecklistItem *newChecklistItem = [CCChecklistItem new];
newChecklistItem.Id = 2;
newChecklistItem.Description = @"This is the Description";
newChecklistItem.RoleId = 2;
newChecklistItem.GroupId = 3;
newChecklistItem.Notes = nil;
newChecklistItem.Status = Completed;
XCTestExpectation *checklistItemExpectation = [self expectationWithDescription:@"checklistItem"];
id delegate = OCMProtocolMock(@protocol(ChecklistServiceDelegate));
id mock = [OCMockObject mockForProtocol:(@protocol(IChecklistService))];
[[[mock stub] andReturn:newChecklistItem] createChecklistItem:checklistItem delegate:delegate];
OCMExpect(([delegate didCompleteCreateChecklistItem:[OCMArg checkWithBlock:^BOOL(id obj) {
CCChecklistItem *items = obj;
XCTAssertNotNil(items);
double checklistId = items.Id;
XCTAssert(checklistId != 0);
[checklistItemExpectation fulfill];
}]]));
[_service createChecklistItem:checklistItem delegate:delegate];
[self waitForExpectationsWithTimeout:5 handler:^(NSError *error) {
if(error) {
}
OCMVerifyAll(delegate);
}];
}
但是,我收到错误failed: caught "NSUnknownKeyException", "[<__NSCFString 0x7faeaad6bd60> valueForUndefinedKey:]: this class is not key value coding-compliant for the key CompletedBy."
createChecklistItem定义如下:
- (void)createChecklistItem:(id<IChecklistItem>)checklistItem delegate:(NSObject<ChecklistServiceDelegate> *)delegate
{
NSMutableArray *mockChecklistItem = [NSMutableArray new];
[TestObjectLoader loadObject:mockChecklistItem
fromJSONFile:@"ChecklistItem"
withMapping:[MappingProvider checklistCreateMapping]];
if (delegate != nil && [delegate respondsToSelector:@selector (didCompleteCreateChecklistItem:)]) {
[delegate didCompleteCreateChecklistItem:(NSObject<IChecklistItem> *)mockChecklistItem];
}
}
Checklist.json在哪里
{
"Id": 13,
"Desc": "Checklist Description",
"IsCompleted": "false",
"TypeId": 1,
"RoleId": 1,
"Status": "C",
"CompletedDateTime": "2015-06-23T00:00:00+00:00",
"CompletedBy": "AC",
"Notes": [
{
"Note": "test",
"CreatedBy": "AC",
"CreatedDateTime": "2015-06-23T00:00:00+00:00"
}
]
}
基本上,我想模拟服务,当调用createChecklistItem
时,我只是想在对象上设置一个随机ID并将其传回,而不是通过Json文件中的ChecklistService读取它。无论如何还有吗?我在我的测试中加入了模拟,但我不相信我正确使用它...
答案 0 :(得分:1)
您实际上并不需要Checklist.json
,您应该只返回具有不同ID的值。
因此...
- (void)createChecklistItem:(id<IChecklistItem>)checklistItem delegate:(NSObject<ChecklistServiceDelegate> *)delegate
{
NSMutableArray *mockChecklistItem = [NSMutableArray new];
[TestObjectLoader loadObject:mockChecklistItem
fromJSONFile:@"ChecklistItem"
withMapping:[MappingProvider checklistCreateMapping]];
if (delegate != nil && [delegate respondsToSelector:@selector (didCompleteCreateChecklistItem:)]) {
[delegate didCompleteCreateChecklistItem:(NSObject<IChecklistItem> *)mockChecklistItem];
}
}
应该只是
checklistItem.Id = 2; //Random number