我尝试对协议方法进行存根处理,但返回的结果为nil。请检查以下代码。
@protocol Client
- (Account* _Nullable) login:(nullable NSString*)username
password:(nonnull NSData*)login;
我有一个名为ClientImplementation的对象,该对象实现了Client协议。
在我的测试用例中,我在setup()中模拟了这样的类。
@property(nonatomic, strong) ClientImplementation<Client> *mockClient;
self.mockClient = mockObjectAndProtocol([ClientImplementation class],@protocol(Client));
但是当我对方法进行Stub调用时,它将返回nil。
Account *account = [[Account alloc]init];
account.name = @"fdsafdsfs";
[given([self.mockClient login:@""passwrod:anything()]) willReturn:account];
我可以知道我做错了什么吗
答案 0 :(得分:0)
OCMockito的默认行为是,除非另行指定,否则方法将返回nil
。这是您指定的内容:
given([self.mockClient login:@"" password:anything()]) willReturn:account
这告诉OCMockito在使用匹配参数调用account
时返回login:password:
。 anything()
将匹配任何内容,但是@""
将仅匹配一个空字符串。我怀疑您的测试正在使用其他登录名进行呼叫。
在given
语句中指定所需的登录名。实际上,您可以根据需要让不同的登录名返回不同的帐户。