Skip to content

Instantly share code, notes, and snippets.

@ddribin
Created October 2, 2009 13:50
Show Gist options
  • Save ddribin/199758 to your computer and use it in GitHub Desktop.
Save ddribin/199758 to your computer and use it in GitHub Desktop.
@interface AClassThatUsesNSWorkspace : NSObject
{
NSWorkspace * _workspace;
}
// Production code uses this
- (id)init;
// Useful for testing (to inject a mock NSWorkspace)
- (id)initWithWorkspace:(NSWorkspace *)workspace;
- (void)openFile;
@end
@implementation AClassThatUsesNSWorkspace
- (id)init
{
return [self initWithWorkspace:[NSWorkspace sharedWorkspace]];
}
- (id)initWithWorkspace:(NSWorkspace *)workspace
{
self = [super init];
if (self == nil)
return nil;
_workspace = [workspace retain];
return self;
}
- (void)dealloc
{
[_workspace release];
[super dealloc];
}
- (void)openFile
{
[_workspace openFile:@"file" withApplication:@"app" andDeactivate:YES];
}
@end
@interface AClassThatUsesNSWorkspaceTest : SenTestCase
@end
@implementation AClassThatUsesNSWorkspaceTest
- (void)testOpenFile
{
id mockWorkspace = [OCMockObject mockForClass:[NSWorkspace class]];
AClassThatUsesNSWorkspace * sut = [[[AClassThatUsesNSWorkspace alloc] initWithWorkspace:mockWorkspace] autorelease];
[[mockWorkspace expect] openFile:@"file" withApplication:@"app" andDeactivate:YES];
[sut openFile];
[mockWorkspace verify];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment