0

不确定我是否正确使用了 OCMocks,或者是因为我使用的是 cocoapods 版本而不是 .lib 文件。

这是测试

#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];
}

@结尾

这是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];
}

googleTrends 当然是标题中的一个属性:

 @property (nonatomic, strong) MWGoogleTrends *googleTrends;

我会想象它正在被调用,因为它下面的以下测试正在通过:

- (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

也从编译器得到这个警告,不确定它是否以任何方式相关:

ld: warning: directory not found for option '-L/Users/markw/Projects/XCode/OReillyCasts/MWGoogleTrends/Pods/build/Release-iphoneos'
4

1 回答 1

0

是的,问题在于存根方法。

我必须只为需要存根的测试方法在模拟上存根该方法,当我从设置中取出存根时,期望通过了。

于 2012-11-07T20:45:02.557 回答