OCMock:设定对物业的期望

时间:2014-11-26 08:22:56

标签: objective-c ocmock

如何设置实例属性的期望值?

假设我有以下代码:

id request = OCMClassMock([NSMutableURLRequest class]);

我想确保在我的实现中,HTTPMethod属性设置为@"Get",我的测试将如何验证?

1 个答案:

答案 0 :(得分:2)

尝试这样的事情:

- (void)testNSURLConnection
{
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.stackoverflow.com"]];
    [request setHTTPMethod:@"POST"];

    id connectionMock = OCMClassMock([NSURLConnection class]);
    OCMStub([connectionMock connectionWithRequest:[OCMArg checkWithBlock:^BOOL(NSMutableURLRequest *request) {
        XCTAssertTrue([request.HTTPMethod isEqualToString:@"GET"]);
        return YES;
    }] delegate:OCMOCK_ANY]);

    [NSURLConnection connectionWithRequest:request delegate:nil];
}

此测试将失败,直到您将@"POST"更改为@"GET",我相信,这是您想要的。