For my project at wanted to do it a bit TDD, although I'm quite new to Objective C and unit testing. I've created a project which connects to a web service which returns a json response. Now I've created a unit test to mock the web service and it keeps crashing (not even failing), and I'm totally lost at the moment....
I've got the following setup:
SignUpService
(to create a new account) which uses ServiceHelper
(which makes the actual http web service requests). Like this ServiceHelper.h
:
@protocol ServiceHelperProtocol <NSObject>
@required
- (NSString *)get:(NSString *)url;
@end
@interface ServiceHelper : NSObject <ServiceHelperProtocol> {
NSMutableData *receivedData;
}
And SignUpService.h
@class ServiceHelper;
@protocol SignUpServiceProtocol <NSObject>
- (NSString *)createUserAccount:(CreateUserAccountRequest *)createUserAccountRequest;
- (bool)doesUsernameExist:(NSString *)userName;
- (BOOL)isEmailValid:(NSString *)email;
- (BOOL)doesEmailExist:(NSString *)email;
@end
@interface SignUpService : NSObject <SignUpServiceProtocol> {
id <ServiceHelperProtocol> serviceHelper;
}
@property(strong) id <ServiceHelperProtocol> serviceHelper;
- (id)initWithHelper:(id <ServiceHelperProtocol>)myServiceHelper;
Next are the unit tests (SignUpServiceTests.h and implementation)
@interface SignUpServiceTests : SenTestCase {
id <NSObject, ServiceHelperProtocol> serviceHelper;
SignUpService *signUpService;
CreateUserAccountRequest *createUserAccountRequest;
}
@property(nonatomic, strong) SignUpService *signUpService;
@property(nonatomic, strong) CreateUserAccountRequest *createUserAccountRequest;
@property(nonatomic, strong) id <NSObject, ServiceHelperProtocol> serviceHelper;
And the implementation:
@implementation SignUpServiceTests
@synthesize signUpService;
@synthesize createUserAccountRequest;
@synthesize serviceHelper;
- (void)setUp {
[super setUp];
// Set-up code here.
self.createUserAccountRequest = [[CreateUserAccountRequest alloc] init];
self.createUserAccountRequest.firstName = @"first-name";
self.createUserAccountRequest.lastName = @"last-name";
.....
self.serviceHelper = [ServiceHelper new];
self.signUpService = [[SignUpService alloc] initWithHelper:(id <ServiceHelperProtocol>) self.serviceHelper];
}
- (void)tearDown {
// Tear-down code here.
[super tearDown];
}
- (void)testOnCreateUserAccountShouldReturnCreatedUserIdWhenCorrectResponseFromService {
id mock = [OCMockObject partialMockForObject:(NSObject *) (id <ServiceHelperProtocol>) self.serviceHelper];
[[[mock stub] andReturn:@"{\"status\":\"ok\",\"create_user\":\"12\"}"] get:[OCMArg any]];
assertThat([self.signUpService createUserAccount:self.createUserAccountRequest], equalTo(@"12"));
}
The project contains OCMock version 1.77 and OCHamcrest libraries.
For some reason the test keeps crashing with "Process finished with exit code 0". When I'm debugging I can't see what's going wrong. I feel it has something to do with memory management, or has it to do with OCMock and ARC ? (like suggested here and here)
Any thoughts or suggestions for a newbie Objective C developer ?