Unit testing with `OCMock`

I want to cover a class MYImageLoader

with unit tests using OCMock. In your tests, make sure that when the image is loaded, it copies the file from the location returned in the callback NSURLSessionDownloadTask

to a temporary location. And here I am trying to write a good unit test I don't want to check the implementation details of the method. So I am passing the mock of the class NSFileManager

in the constructor of the class MYImageLoader

. But there are several ways 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 good practice here? I don't want my unit tests to determine which methods I should use in the method implementation to make sure the test passes.

Is there something like this or this in OCMock

?

Or maybe you can suggest a different approach?

+3


source to share


1 answer


What if, instead of waiting, you just make fun of, but add an andDo block that sets a bool when it does?

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

      



If you do it for everyone and then claim that it is YES, that might take care of what you want to do.

+1


source







All Articles