Stubbing Protocol return as nil OCMockito

101 Views Asked by At

I've tried to stub the protocol method but it return as nil. Please check the following code.

@protocol Client
- (Account* _Nullable) login:(nullable NSString*)username
                                        password:(nonnull NSData*)login;

And I have a object called ClientImplementation that implements Client protocol.

In my testcase, I mock the class like this in setup().

@property(nonatomic, strong) ClientImplementation<Client> *mockClient;
 self.mockClient =  mockObjectAndProtocol([ClientImplementation class],@protocol(Client));

But when i Stub the method it return as nil.

Account *account = [[Account alloc]init];
    account.name = @"fdsafdsfs";
    [given([self.mockClient login:@""passwrod:anything()]) willReturn:account];

May I know what did i do wrong?

1

There are 1 best solutions below

0
On

OCMockito's default behavior is that methods return nil unless you specify otherwise. Here's what you've specified:

given([self.mockClient login:@"" password:anything()]) willReturn:account

This tells OCMockito to return account when login:password: is called with matching parameters. anything() will match anything, but @"" will only match an empty string. I suspect that your test is calling with a different login.

Specify the desired login in the given statement. In fact, you can have different logins return different accounts, if you want.