1

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 ?

Community
  • 1
  • 1

1 Answers1

0

Without having further information I would suspect that it's not because of a memory problem but because the loader can't find the OCMock framework when octest is launched. In OS X the linker (at build time) time uses different means to locate frameworks than the loader (at runtime).

Public releases of OCMock are built with @rpath in the loader path. This allows for a fair degree of flexibility with the project layout at runtime, but it can be confusing at first. As a first step, can you make sure to set the LC_RPATH environment variable to include a directory in which the OCMock framework is available? The discussion of @rpath on the dyld man page has more detail.

By the way, OCMock 2.0 was released recently.

Erik Doernenburg
  • 2,933
  • 18
  • 21
  • Hi Erik, thanks for the comment. I'm not quite into this compiler / environment settings, can you help me out where I need to put these variable or @rpath in Xcode ? – Rutger van Dijk Apr 17 '12 at 15:21