[英]Stubbing Protocol return as nil OCMockito
I've tried to stub the protocol method but it return as nil. 我试图对协议方法进行存根处理,但返回的结果为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. 我有一个名为ClientImplementation的对象,该对象实现了Client协议。
In my testcase, I mock the class like this in setup(). 在我的测试用例中,我在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? 我可以知道我做错了什么吗?
OCMockito's default behavior is that methods return nil
unless you specify otherwise. OCMockito的默认行为是,除非另行指定,否则方法将返回
nil
。 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. 这告诉OCMockito在使用匹配参数调用
login:password:
时返回account
。 anything()
will match anything, but @""
will only match an empty string. anything()
将匹配任何内容,但是@""
仅匹配一个空字符串。 I suspect that your test is calling with a different login. 我怀疑您的测试正在使用其他登录名进行呼叫。
Specify the desired login in the given
statement. 在
given
语句中指定所需的登录名。 In fact, you can have different logins return different accounts, if you want. 实际上,您可以根据需要让不同的登录名返回不同的帐户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.