jonreid / OCMockito

Mockito for Objective-C: creation, verification and stubbing of mock objects
MIT License
991 stars 118 forks source link

Stubbing the protocol return as Nil #151

Open AyeChanPyaeSone opened 5 years ago

AyeChanPyaeSone commented 5 years ago

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?

jonreid commented 5 years ago

I'm sorry you've waited so long for an answer — I don't seem to be receiving any notifications from GitHub.

For all mocked methods, nil is returned unless the given condition is met. My guess is that there's something about your given that doesn't match what your code calls. You are using anything() to match any password argument, so that's fine. Is the code passing empty string for login?

jasongrlicky commented 4 years ago

I just wanted to mention that I've had the same problem when the matched-against or returned objects are typedefs. Might give a clue about what's going on?

jonreid commented 4 years ago

Thank you, @jasongrlicky. If you get a chance and are able to boil it down to an executable example, that would be really helpful.