0
不知道我是否正確使用OCMocks,或者是因爲我使用的是cocoapods版本而不是.lib文件。爲什麼這個OCMock驗證失敗?
這裏的測試(多個)
#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
這裏的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'
你看到了什麼問題? –
OCMockObject [MWGoogleTrends]:未調用期望的方法:getLatestTrends。 ETA:以上更新顯示其下方的測試已通過,因此ViewDidLoad正在被調用,並且正在返回存根結果。 –
我認爲(自從我實際使用OCMock以來已經有一段時間了),這是因爲您在設置期望調用同一個存根方法之前將該方法存根存根。 – InsertWittyName