0

Not sure if I'm using OCMocks correctly, or if it's because I'm using the cocoapods version and not the .lib file.

Here's the test(s)

#import "MWViewControllerTests.h"
#import "MWViewController.h"
#import "OCMockObject.h"
#import "MWGoogleTrends.h"
#import "OCMockRecorder.h"

@implementation MWViewControllerTests {

    MWViewController *vcSUT;
    id googleTrends;
    NSArray *trends;
}

- (void)setUp
{
    [super setUp];
    vcSUT = [[MWViewController alloc] init];
    googleTrends = [OCMockObject mockForClass:[MWGoogleTrends class]];
    vcSUT.googleTrends = googleTrends;
    trends = [[NSArray alloc] initWithObjects:@"trend1", @"trend2", @"trend3", nil];
    [[[googleTrends stub] andReturn:trends] getLatestTrends];
}

- (void)tearDown
{
    // Tear-down code here.
    [super tearDown];
}

- (void)test_ViewDidLoadShouldCallGoogleTrends_getLatestTrends
{
    [[googleTrends expect] getLatestTrends];
    [vcSUT view];  // calls loadView
    [googleTrends verify];
}

@end

Here's the VC:

@implementation MWViewController {

    NSArray *_trends;
}

@synthesize googleTrends;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _trends = [self.googleTrends getLatestTrends];
}

The googleTrends is a property in the header of course:

 @property (nonatomic, strong) MWGoogleTrends *googleTrends;

I would imagine it's being invoked because the following tests below it are passing:

- (void)test_numRowsInTableViewShouldBeNumOfTrendsReturnedFromGetLatestTrends {

    [vcSUT view];
    int numSections = [vcSUT tableView:nil numberOfRowsInSection:0];
    STAssertEquals(numSections, 3, @"should be 3 sections");
}

- (void)test_cellForRowAtIndexPath_should_return_cell_with_trendName_as_text {

    [vcSUT view];
    UITableViewCell *cell1 = [vcSUT tableView:nil cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    UITableViewCell *cell2 = [vcSUT tableView:nil cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:1]];
    UITableViewCell *cell3 = [vcSUT tableView:nil cellForRowAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:2]];

    STAssertEquals(cell1.textLabel.text, @"trend1", @"cell text should be correct trend name");
    STAssertEquals(cell2.textLabel.text, @"trend2", @"cell text should be correct trend name");
    STAssertEquals(cell3.textLabel.text, @"trend3", @"cell text should be correct trend name");
}

@end

also getting this warning from the compiler, not sure if it related in any way:

ld: warning: directory not found for option '-L/Users/markw/Projects/XCode/OReillyCasts/MWGoogleTrends/Pods/build/Release-iphoneos'
Mark W
  • 3,879
  • 2
  • 37
  • 53
  • What is the problem you're seeing? – Ben Scheirman Nov 07 '12 at 16:58
  • OCMockObject[MWGoogleTrends]: expected method was not invoked: getLatestTrends. ETA: updated above to show that the tests below it are passing, so ViewDidLoad is being called and the stubbed results are being returned. – Mark W Nov 07 '12 at 19:39
  • I think (it's been a while since I actually used OCMock) that it's because you've stubbed the method previous to setting up the expectation that the same stub method is going to be called. – InsertWittyName Nov 07 '12 at 20:07

1 Answers1

0

Yes, the issue is with stubbing the method.

I had to stub the method on the mock only for the test methods that needed the stub, the expectation passes when I took out the stubbing from the Setup.

Mark W
  • 3,879
  • 2
  • 37
  • 53