OCMock 的意外行为期望 iOS 单元测试

Unexpected behaviour of OCMock expect iOS Unit test

我有一个实现如下的方法:

- (IBAction)loginButtonTapped:(id)sender
{

    void (^SuccessBlock)(id, NSDictionary*) = ^(id response, NSDictionary* headers) {

    };
    void (^ErrorBlock)(id, NSDictionary*, id) = ^(NSError* error, NSDictionary* headers, id response) {


    };
   [ClassName deleteWebService:@"www.google.com"
                                           data:nil
                               withSuccessBlock:SuccessBlock
                                 withErrorBlock:ErrorBlock];
}

为此我有一个测试用例如下:

- (void)testLoginButtonTapped
{
    id mockClassObj = [OCMockObject mockForClass:[ClassName class]];

    void (^SuccessBlock)(id, NSDictionary*) = ^(id response, NSDictionary* headers) {

    };
    void (^ErrorBlock)(id, NSDictionary*, id) = ^(NSError* error, NSDictionary* headers, id response) {

    };

    [[mockClassObj expect] deleteWebService:@"www.google.com"
                                        data:nil
                            withSuccessBlock:SuccessBlock
                              withErrorBlock:ErrorBlock]; //code 1



    [[mockClassObj expect] putWebService:@"www.google.com"
                                     data:nil
                         withSuccessBlock:SuccessBlock
                           withErrorBlock:ErrorBlock]; //code 2

// putWebService is the another method of the same class

[mockObj loginButtonTapped:[OCMockObject niceMockForClass:[UIButton class]]];
        [mockObj verify];
    }

代码 1 和代码 2 都给出 "Test Succeeded",代码 1 很明显,但我不明白为什么代码 2 会出现这种情况。

您正在使用 expect-运行-verify 方法(参见 http://ocmock.org/reference/#strict-mocks-and-expectations),并且您正在 mockClassObject 上设置期望但你永远不会在 mockClassObj 上调用 verify。这意味着代码 1 和代码 2 中的期望从未得到验证,因此测试不会失败。