OCMock: Setting expectations on properties

305 views Asked by At

How do I set an expectation on a property of an instance?

Let's say I have the following code:

id request = OCMClassMock([NSMutableURLRequest class]);

And I want to make sure that, in my implementation, the HTTPMethod property is set to @"Get", how would my test verify this?

1

There are 1 answers

1
Ben Flynn On BEST ANSWER

Try something like this:

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

This test will fail until your change @"POST" to @"GET" which is, I believe, what you want.