MrJre
MrJre

Reputation: 7161

How to test a simple data object class which implements NSCoding?

I have a small data object that needs to be serialized and deserialized. Lets say it is called WeatherDetails, and it looks like this:

WeatherDetails.h

@interface WeatherDetails : NSObject <NSCoding>
{
@private

@protected
}

#pragma mark - Properties

@property (nonatomic, copy) NSString *weatherCode;
@property (nonatomic, copy) NSString *weatherDescription;

@end

WeatherDetails.m

#import "WeatherDetails.h"

@implementation WeatherDetails

NSString *const WEATHER_DETAILS_WEATHER_CODE_KEY = @"s";
NSString *const WEATHER_DETAILS_WEATHER_DESCRIPTION_KEY = @"sT";

#pragma mark - Initialization, NSCoding and Dealloc

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    _weatherCode = [aDecoder decodeObjectForKey:@"weatherCode"];
    _weatherDescription = [aDecoder decodeObjectForKey:@"weatherDescription"];

    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:_weatherCode forKey:@"weatherCode"];
    [aCoder encodeObject:_weatherDescription forKey:@"weatherDescription"];
}

Currently my tests look like this;

#import <XCTest/XCTest.h>
#import <OCMock/OCMock.h>
#import "WeatherDetails.h"
@interface WeatherDetailsTests : XCTestCase

@end

@implementation WeatherDetailsTests

- (void)testThatWeatherCodeIsEncoded
{
    WeatherDetails *details = [[WeatherDetails alloc] init];
    [details setWeatherCode:@"A"];

    NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:details];

    WeatherDetails *unarchive = [NSKeyedUnarchiver unarchiveObjectWithData:archive];

    XCTAssertEqualObjects(@"A", [unarchive weatherCode]);
}

- (void)testThatWeatherDescriptionIsEncoded
{
    WeatherDetails *details = [[WeatherDetails alloc] init];
    [details setWeatherDescription:@"A"];

    NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:details];

    WeatherDetails *unarchive = [NSKeyedUnarchiver unarchiveObjectWithData:archive];

    XCTAssertEqualObjects(@"A", [unarchive weatherDescription]);
}

I have a gut feeling that this approach to testing if all properties are correctly encoded is not really optimal as there is duplication, but I can't really think of a better approach. Does anyone have a tip for me on improving this?

Upvotes: 4

Views: 1761

Answers (2)

MrJre
MrJre

Reputation: 7161

In the end I improved it by applying a custom assertion;

When To Use It

We should consider creating a Custom Assertion whenever any of the following are true:

  • We find ourselves writing (or cloning) the same assertion logic in test after test
  • We find ourselves writing Conditional Test Logic in the result verification part of our tests. That is, our calls to Assertion Methods are embedded in if statements or loops.
  • The result verification parts of our tests are suffering from Obscure Test because we are using procedural rather than declarative result verification in the tests.
  • We find ourselves doing Frequent Debugging whenever assertions fail because they do not provide enough information.

The custom assertion currently looks like this:

/*!
 * @define XCTAssertEqualSerialized(value, object, selector)
 * Serializes (\a object), then desirializes it and compares result's property (\a propertyName) to (\a value) with XCTAssertEqualObjects
 * @param value Value to compare.
 * @param object Object to serialize.
 * @param selector Objects property to compare.
 */

#define XCTAssertEqualSerialized(value, object, selector) \
({ \
NSData *archived = [NSKeyedArchiver archivedDataWithRootObject:object]; \
NSObject *unarchived = [NSKeyedUnarchiver unarchiveObjectWithData:archived]; \
XCTAssertEqualObjects(value, [unarchived valueForKeyPath:NSStringFromSelector(selector)]); \
})

/*!
 * @define XCTAssertNotNilSerialized(object, selector)
 * Serializes (\a object), then desirializes it and checks result's property (\a propertyName) is not nil with XCTAssertNotNil
 * @param object Object to serialize.
 * @param selector Objects property to compare.
 */

#define XCTAssertNotNilSerialized(object, selector) \
({ \
NSData *archived = [NSKeyedArchiver archivedDataWithRootObject:object]; \
NSObject *unarchived = [NSKeyedUnarchiver unarchiveObjectWithData:archived]; \
XCTAssertNotNil([unarchived valueForKeyPath:NSStringFromSelector(selector)]); \
})

Which results in the tests looking like this:

- (void)testThatWeatherCodeIsEncoded
{
    WeatherDetails *details = [[WeatherDetails alloc] init];
    [details setWeatherCode:@"A"];

    XCTAssertEqualSerialized(@"A", details, @selector(weatherCode));
}

- (void)testThatWeatherDescriptionIsEncoded
{
    WeatherDetails *details = [[WeatherDetails alloc] init];
    [details setWeatherDescription:@"A"];

    XCTAssertEqualSerialized(@"A", details, @selector(weatherDescription));
}

Upvotes: 2

Kevin
Kevin

Reputation: 17576

What you really want to test is that the object is same before and after you archive it.

Implement a method in your WeatherDetails to compare the objects (or override isEqual:).

- (BOOL)isEqualToWeatherDetails:(WeatherDetails *)details
{
    if (![details isKindOfClass:[WeatherDetails class]]) return NO;
    return [self.weatherCode == details.weatherCode && self.weatherDescription isEqualToString:details.weatherDescription];
}

Then you can do all your equality comparisons at once:

- (void)testNSCoder
{
    WeatherDetails *details = [[WeatherDetails alloc] init];
    [details setWeatherCode:@"A"];
    details.weatherDescription = @"Cloudy";

    NSData *archive = [NSKeyedArchiver archivedDataWithRootObject:details];

    WeatherDetails *unarchive = [NSKeyedUnarchiver unarchiveObjectWithData:archive];

    XCTAssertTrue([details isEqualToWeatherDetails:unarchive]);
}

If you overrode isEqual: then you could compare doing this:

XCTAssertEqualObjects(details, unarchive);

Apple tends to add additional methods (isEqualToArray:, isEqualToDictionary:). isEqual: is used by collections like NSSet and NSDictionary

Upvotes: 3

Related Questions