Skip to content

Instantly share code, notes, and snippets.

@whalec
Created July 20, 2011 09:37
Show Gist options
  • Save whalec/1094667 to your computer and use it in GitHub Desktop.
Save whalec/1094667 to your computer and use it in GitHub Desktop.
Thoughts on how the API for class mocking in ObjectiveC should look
// I'd think that if NSString doesn't get sent stringWithFormat here before the end of this test, it should raise an error? It's mocking after all, not stubbing.
- (void)testClassMethodMocking {
[NSString mock:@selector(stringWithFormat:) andReturn:@"someString"];
NSString *string = [NSString stringWithFormat@"abc123"];
GHAssertEqual(string, @"someString", "NSString hasn't been mocked")
}
// This guy wouldn't raise.
- (void)testClassMethodStubbing {
[NSString stub:@selector(stringWithFormat:) andReturn:@"someString"];
NSString *string = [NSString stringWithFormat@"abc123"];
GHAssertEqual(string, @"someString", "NSString hasn't been mocked")
}
// Not too sure how to pass argument assertions though
- (void)testClassMethodMockingWithArguments {
NSDate *date = [NSDate date];
[NSString mock:@selector(stringWithFormat) withArgs:date, nil andReturn:@"11-06-2011"];
NSString *string = [NSString stringWithFormat:@"%@", date];
GHAssertEqual(string, @"11-06-2011", "NSString hasn't been mocked");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment