It’s stepping into the ViewDidLoad of the main view controller, and hitting the line calling get all tweets, but I put a breakpoint in the getAllTweets of both the base and derived to see if it just wasn’t hitting the derived like I expected.
@implementation WWMainViewControllerTests {
// system under test
WWMainViewController *viewController;
// dependencies
UITableView *tableViewForTests;
WWTweetServiceMock *tweetServiceMock;
}
- (void)setUp {
tweetServiceMock = [[WWTweetServiceMock alloc] init];
viewController = [[WWMainViewController alloc] init];
viewController.tweetService = tweetServiceMock;
tableViewForTests = [[UITableView alloc] init];
viewController.mainTableView = tableViewForTests;
tableViewForTests.dataSource = viewController;
tableViewForTests.delegate = viewController;
}
- (void)test_ViewLoadedShouldCallServiceLayer_GetAllTweets {
[viewController loadView];
STAssertTrue(tweetServiceMock.getAllTweetsCalled, @"Should call getAllTweets on tweetService dependency");
}
- (void)tearDown {
tableViewForTests = nil;
viewController = nil;
tweetServiceMock = nil;
}
The base tweet service:
@implementation WWTweetService {
NSMutableArray *tweetsToReturn;
}
- (id)init {
if (self = [super init]) {
tweetsToReturn = [[NSMutableArray alloc] init];
}
return self;
}
- (NSArray *)getAllTweets {
NSLog(@"here in the base of get all tweets");
return tweetsToReturn;
}
@end
The Mock tweet service:
@interface WWTweetServiceMock : WWTweetService
@property BOOL getAllTweetsCalled;
@end
@implementation WWTweetServiceMock
@synthesize getAllTweetsCalled;
- (id)init {
if (self = [super init]) {
getAllTweetsCalled = NO;
}
return self;
}
- (NSArray *)getAllTweets {
NSLog(@"here in the mock class.");
getAllTweetsCalled = YES;
return [NSArray array];
}
The main view controller under test:
@implementation WWMainViewController
@synthesize mainTableView = _mainTableView;
@synthesize tweetService;
NSArray *allTweets;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
allTweets = [tweetService getAllTweets];
NSLog(@"was here in view controller");
}
- (void)viewDidUnload
{
[self setMainTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
Since you’re able to break in the debugger in
viewDidLoad, what’s the value of thetweetServiceivar? If it’snil, thegetAllTweetsmessage will just be a no op. Maybe the ivar isn’t being set properly or overridden somewhere else.You should probably use the property to access the
tweetService(callself.tweetService) rather than its underlying ivar. You should only ever access the ivar directly in getters, setters, andinit(alsodeallocif aren’t using ARC for some crazy reason).You also should not call
loadViewyourself, rather just access theviewproperty of the view controller. That will kick off the loading process and callviewDidLoad.Also, if you’re doing a lot of mocking, I highly recommend OCMock.