1

我正在将 OCMock 与 GHUnit 结合使用,以尝试从测试驱动的 iOS 开发中重新创建 Graham Lee 的“BrowseOverflow”项目。

我的理解是模拟任何不是您正在测试的当前类的对象。我正在使用我的 Question 类,该类依赖于我的 Answer 类的某些功能。

问题.h

@class Answer;

@interface Question : NSObject {
    NSMutableSet *answerSet;
}

@property (retain) NSDate *date;
@property NSInteger score;
@property (copy) NSString *title;
@property (readonly) NSArray *answers;

- (void)addAnswer:(Answer *)answer;

@end

问题.m

#import "Question.h"
#import "Answer.h"

@implementation Question

@synthesize date;
@synthesize score;
@synthesize title;

- (id)init
{
    if ((self = [super init])) {
        answerSet = [[NSMutableSet alloc] init];
    }
    return self;
}

- (void)addAnswer:(Answer *)answer
{
    [answerSet addObject:answer];
}

- (NSArray *)answers
{
    return [[answerSet allObjects] sortedArrayUsingSelector:@selector(compare:)];
}

@end

答案.h

#import <Foundation/Foundation.h>

@class Person;

@interface Answer : NSObject

@property (readwrite) NSString *text;
@property (readwrite) Person *person;
@property (readwrite) NSInteger score;
@property (readwrite, getter = isAccepted) BOOL accepted;

- (NSComparisonResult)compare:(Answer *)otherAnswer;

@end

答案.m

#import "Answer.h"

@implementation Answer

@synthesize text;
@synthesize person;
@synthesize score;
@synthesize accepted;

- (NSComparisonResult)compare:(Answer *)otherAnswer
{
    if (accepted && !(otherAnswer.accepted)) {
        return NSOrderedAscending;
    } else if (!accepted && otherAnswer.accepted) {
        return NSOrderedDescending;
    }
    if (score > otherAnswer.score) {
        return NSOrderedAscending;
    } else if (score < otherAnswer.score) {
        return NSOrderedDescending;
    }
    return NSOrderedSame;
}

@end

我在测试时尝试使用 OCMock 代替 Answer 的实例,但它只在大约 10% 的时间内工作。

- (void)testHighScoreAnswerBeforeLow
{
    lowScore = [OCMockObject mockForClass:[Answer class]];
    [[[lowScore stub] andReturn:OCMOCK_VALUE((NSInteger) {4})] score];
    [question addAnswer:lowScore];

    highScore = [OCMockObject mockForClass:[Answer class]];
    [[[highScore stub] andReturn:OCMOCK_VALUE((NSInteger) {-4})] score];
    [question addAnswer:highScore];

    [[lowScore expect] compare:[OCMArg any]];

    NSArray *answers = question.answers;
    NSInteger highIndex = [answers indexOfObject:highScore];
    NSInteger lowIndex = [answers indexOfObject:lowScore];
    GHAssertTrue(highIndex < lowIndex, @"High-scoring index comes first");

    [highScore verify];
    [lowScore verify];
}

我认为问题是由 NSSet 如何在内存中存储对象(有点随机)以及 OCMock 检查以确保没有调用额外的方法这一事实引起的冲突。我通过将 OCMock 排除在此特定测试中来解决问题,但这似乎是一个糟糕的测试。

- (void)testHighScoreAnswerBeforeLow
{
    lowScore = [[Answer alloc] init];
    lowScore.score = -4;
    [question addAnswer:lowScore];

    highScore = [[Answer alloc] init];
    highScore.score = 4;
    [question addAnswer:highScore];

    NSArray *answers = question.answers;
    NSInteger highIndex = [answers indexOfObject:highScore];
    NSInteger lowIndex = [answers indexOfObject:lowScore];
    GHAssertTrue(highIndex < lowIndex, @"High-scoring index comes first");
}

当您不知道 OCMock 需要进行多少次比较时,是否可以让 OCMock 与排序算法配合得很好?如果不是,这是使用 OCMock 的一个缺点,还是它的代码结构不佳?

4

1 回答 1

2

问题是您对比较设定了期望。如果你不在乎它被调用了多少次,你不应该告诉模拟你希望它被调用一次。

如果您不关心在模拟对象上调用了哪些意外方法,则可以使用 nicemock。

您可以使用存根仅返回某个值,而不关心它被调用了多少次。

于 2013-05-08T20:43:41.197 回答