OCMock设置模拟类,其属性并获得基本的理解

时间:2014-11-07 14:15:20

标签: ios objective-c testing tdd ocmock

我真的很喜欢这个TDD的东西并且总体上编写测试用例但是我也非常渴望进入它并理解。尽管目前我很难用咒语和嘲弄来理解语法和思维方式。

但是我也知道我只需要掌握它的基本内容就可以获得" aaah"时刻。

Atm我不在那里。 所以我知道这是非常基本的,这是我的理由:

我有一个汽车作为CoreData的类对象。 init是ofc

[MyCar alloc] initWithEntity:<(NSEntityDescription *)> insertIntoManagedObjectContext:<(NSManagedObjectContext *)>

我想测试一下:

- (CLLocationCoordinate2D) locationCoordinatesAtIndex: (NSInteger) index
{

CLLocationCoordinate2D returnValue
= CLLocationCoordinate2DMake(0.0f, 0.0f);

/* Sanity check */
if (index >= 0 && index < self.carsList.count)
{
    MyCar* coolCar = self.carsList[index]; //array with MyCar objects
    returnValue = CLLocationCoordinate2DMake(self.coolCar.latitude.doubleValue,
                                             self.coolCar.longitude.doubleValue);
}
return returnValue;
}

此方法存在于CarViewController

所以我在想 - 好的,我要模仿MyCar CarViewController并向self.coolCar.latitudeself.coolCar.longitude添加固定值:

@interface CarViewController
@property (nonatomic, strong) MyCar *coolCar;
@property (nonatomic, strong) NSMutableArray *carsList; //because existing private in .m
@end

@interface MyCar (Test)
@property (nonatomic, strong) NSNumber *latitude; //because existing private in .m
@property (nonatomic, strong) NSNumber *longitude; //because existing private in .m
@end

@interface WTSTestCarLocation : XCTestCase
@property (nonatomic, strong) CarViewController *carVC;
@end


-(void)testCarLoc{

self.carVC = OCMClassMock([WTSARViewController class]);`
self.coolCar = [OCMockObject mockForClass:[MyCar class]]`


self.coolCar.latitude = @7.05444;
self.coolCar.longitude = @125.601087;
[self.carsLists addObject:self.coolCar];


OCMStub([self.carVC locationCoordinatesAtIndex:0]);
/* atm I just log */
NSLog(@"the location2DLat %f",[self.carVC locationCoordinatesAtIndex:0].latitude);`

}

我在编译器说&#34; doesNotRecognizeSelector:&#34;在线失败

self.coolCar.latitude = @7.05444;

所以问:我需要做一个局部模型吗?以及如何使用NSEntityDescriptionNSManagedObjectContext

任何帮助,指示等都表示赞赏

1 个答案:

答案 0 :(得分:1)

纬度和经度是否可能是初始定义中的只读属性而没有定义setter?

你基本上有正确的想法。您不应该存储实际尝试测试的方法,并且通常不需要测试具有自己的成员变量,您可以在测试方法本身中创建局部变量。

我建议使用这种方法,在OCMock 3.x语法中注明:

-(void)testCarLoc 
{
    // Create test data
    double testLatitude = 7.05444;
    double testLongitude = 125.601087;

    // Use a partial mock for the car object to "set" its private property
    id *carPartial = OCMPartialMock([MyCar new]);
    OCMStub([carPartial latitude]).andReturn(@(testLatitude);
    OCMStub([carPartial longitude).andReturn(@(testLongitude));

    // Use a partial mock for the object under test to "set" its private property
    id *carVCPartial = OCMPartialMock([CarViewController new]);
    OCMStub([carVCPartial carsList]).andReturn(@[carPartial]);

    // Perform the action you want to test 
    CLLocationCoordinate2D result = [carVCPartial locationCoordinatesAtIndex:0];

    // Verify the results
    XCTAssertEquals(result.latitude, testLatitude);
    XCTAssertEquals(result.longitude, testLongitude);
}
相关问题