0

So I am attempting to start developing in a more test driven manner learning to use tests and such and as such i have already come across a vexing issue.

I would like to test that a function i pass an NSArray to then set's an exposed UIScrollview subview third index(first two are default for scroll insets) to be a UIImageView that contains a valid image.

The array i pass in will be of type ALAsset that the method will pull the image out. But i havent gotten that far.

So my first issue is...can i mock an UIImage to be valid i.e not nil in this scenario? or should how I write my tests be approached differently?

so far this is what i have tried

UIImage *mockImage = mock([UIImage class]);
UIImageView *imgView = [[UIImageView alloc]init];

[imgView setImage:mockImage];
NSArray *assets = @[imgView];
[SUT loadImagesForAssets:assets];

NSInteger firstViewIndexOddset = 2;
NSInteger idx = 0;

idx += firstViewIndexOddset;

UIImageView *imgViewFromSUT = [[SUT previewScrollView] subviews][idx];

XCTAssertNotNil(imgViewFromSUT.image);
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
Genhain
  • 1,937
  • 1
  • 19
  • 38
  • Why do you need to mock image? Why not to use any `imageNamed:`? – Eugen Martynov Feb 23 '15 at 09:01
  • Well this is my delve into testing and attempting to understand its nuances, and such and i suppose it is easy enough to use imageNamed: however at some point i will be using ALAsset and that one is a little trickier to just get a valid instance of, so it's why i looked into mocking, and i was just trying it on something a little easier first to see if i could wrap my head around it – Genhain Feb 23 '15 at 16:52

1 Answers1

3

Don't be seduced by mock object frameworks, not even OCMockito. When you're first starting with tests, I recommend not using any mocking framework. Instead, do it all yourself. After you get tired of typing similar things over and over, then see if a mock object framework will help.

When I want a test that uses a valid UIImage, I use this:

@implementation TestingImageData

+ (NSData *)validImageData
{
    const char PNGbytes[] = "\x89\x50\x4E\x47\x0D\x0A\x1A\x0A\x00\x00\x00\x0D\x49\x48\x44\x52\x00"
        "\x00\x00\x01\x00\x00\x00\x01\x01\x03\x00\x00\x00\x25\xDB\x56\xCA\x00\x00\x00\x03\x50\x4C"
        "\x54\x45\x00\x00\x00\xA7\x7A\x3D\xDA\x00\x00\x00\x01\x74\x52\x4E\x53\x00\x40\xE6\xD8\x66"
        "\x00\x00\x00\x0A\x49\x44\x41\x54\x08\xD7\x63\x60\x00\x00\x00\x02\x00\x01\xE2\x21\xBC\x33"
        "\x00\x00\x00\x00\x49\x45\x4E\x44\xAE\x42\x60\x82"; // 1x1 transparent pixel PNG
    size_t PNGlength = sizeof(PNGbytes) - 1;
    return [NSData dataWithBytes:PNGbytes length:PNGlength];
}

@end

For example:

[UIImage imageWithData:[TestingImageData validImageData]]
Jon Reid
  • 20,545
  • 2
  • 64
  • 95
  • Thanks [Jon](http://stackoverflow.com/users/246895/jon-reid), i Recently watched one of your expo talks where you advocate writing your own mocks first. So that's what I'm attempting and thus far it is giving me an slivers of insight into the do's and dont's . I haven't gained instant omnipotent clarity but it's a journey. In saying that, expect to see more questions on mocking and testing before i'm done...if ever – Genhain Mar 02 '15 at 07:25