dariaa
dariaa

Reputation: 6385

Unit testing with `OCMock`

I want to cover MYImageLoader class with unit tests using OCMock. On of the tests makes sure that when it downloads an image it copies the file from the location returned in a NSURLSessionDownloadTask callback to a temporary location. And here I am trying to write a good unit test I do not want to test method implementation details. So I pass a mock of a NSFileManager class in the constructor of MYImageLoader class. But there are several equally possible to use NSFileManager methods to copy a file:

- (BOOL)copyItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError **)error;
- (BOOL)moveItemAtURL:(NSURL *)srcURL toURL:(NSURL *)dstURL error:(NSError **)error;
- (BOOL)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **)error;

So what would be the good practice here? I don’t want my unit tests to determine which methods I should use in the method implementation just to make sure the test passes.

Is there anything like expect this or this or this in OCMock?

Or may be you can advise a different approach?

Upvotes: 3

Views: 281

Answers (1)

Ben Flynn
Ben Flynn

Reputation: 18932

What if, instead of expecting, you just mock, but add an andDo block that sets a bool when it happens?

__block BOOL didMove = NO;
OCMStub([fileManagerMock copyItemAtUrl:OCMOCK_ANY toUrl:OCMOCK_ANY error:[OCMArg setTo:nil]]).andDo(^(NSInvocation *invocation) { didMove = YES; }));

If you do that for each one and then assert it is YES afterward, that might take care of what you want to do.

Upvotes: 1

Related Questions