OCMock 上带有参数并返回值的方法
我有一个依赖于 NSUserDefaults 的类,我正在尝试对其进行单元测试,并且我将 NSUserDefaults 作为模拟提供到我的测试类中。运行测试时,我收到错误:
OCMockObject[NSUserDefaults]: 意外的方法被调用: DictionaryForKey:@"响应"
我试图模拟 NSUserDefaults 类的这个实例方法:
- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;
使用调用格式:
[[[mockClass stub] andReturn:someDictionary] dictionaryForKey:@"aKey"]
告诉模拟它需要期待dictionaryForKey方法。但不知何故,这没有被记录,或者不是告诉模拟期望的正确事情,因为错误告诉我模拟永远不知道期望“dictionaryForKey”调用。
我调用存根的 andReturn 的方式似乎非常相似 对于这个问题,但是在那个问题中,发帖者说他们得到了一个返回值。 我的测试用例:
-(void)testSomeWork
{
id userDefaultsMock = [OCMockObject mockForClass:[NSUserDefaults class]];
MyClass *myClass = [[MyClass alloc] initWith:userDefaultsMock];
NSDictionary *dictionary = [NSDictionary dictionary];
[[[userDefaultsMock stub] andReturn:dictionary] dictionaryForKey:@"response"];
BOOL result = [myClass doSomeWork];
STAssertTrue(result, @"not working right");
[myClass release];
[userDefaultsMock verify];
}
我的班级:
@implementation MyClass
@synthesize userDefaults;
- (id)initWith:(NSUserDefaults *aValue)
{
if (self = [super init])
{
self.userDefaults = aValue;
}
return self;
}
- (BOOL)doSomeWork
{
NSDictionary *response = [userDefaults dictionaryForKey:@"response"];
if (response != nil)
{
// some stuff happens here
return YES;
}
return NO;
}
@end
有什么建议吗?
I have a class that relies on NSUserDefaults that I'm trying to unit-test and I'm providing the NSUserDefaults as a mock into my test class. When running the test, I get the error:
OCMockObject[NSUserDefaults]:
unexpected method invoked:
dictionaryForKey:@"response"
I'm trying to mock this instance method of the NSUserDefaults class:
- (NSDictionary *)dictionaryForKey:(NSString *)defaultName;
using the call format:
[[[mockClass stub] andReturn:someDictionary] dictionaryForKey:@"aKey"]
to tell the mock that it needs to expect the dictionaryForKey method. But somehow, this isn't recorded or isn't the right thing to tell the mock to expect since the error tells me that mock never knew to expect the 'dictionaryForKey' call.
The way I'm invoking the stub's andReturn seems very similar to this question but in that one, the poster says they're getting a return value.
My test case:
-(void)testSomeWork
{
id userDefaultsMock = [OCMockObject mockForClass:[NSUserDefaults class]];
MyClass *myClass = [[MyClass alloc] initWith:userDefaultsMock];
NSDictionary *dictionary = [NSDictionary dictionary];
[[[userDefaultsMock stub] andReturn:dictionary] dictionaryForKey:@"response"];
BOOL result = [myClass doSomeWork];
STAssertTrue(result, @"not working right");
[myClass release];
[userDefaultsMock verify];
}
My class:
@implementation MyClass
@synthesize userDefaults;
- (id)initWith:(NSUserDefaults *aValue)
{
if (self = [super init])
{
self.userDefaults = aValue;
}
return self;
}
- (BOOL)doSomeWork
{
NSDictionary *response = [userDefaults dictionaryForKey:@"response"];
if (response != nil)
{
// some stuff happens here
return YES;
}
return NO;
}
@end
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定您是否弄清楚了这一点,但这可能与使用存根和验证有关。您应该将 verify 与 Expect 一起使用。
即
在这种情况下,您使用 verify 来确认您的方法确实调用了预期的方法(dictionaryForKey:)。您使用存根来允许您的方法调用模拟对象上的给定方法,但您不需要验证它是否被调用。
Not sure if you figured this out but it's probably to do with using stub with verify. You should use verify with expect.
i.e.
In this instance you use verify to confirm that your method did in fact call the expected method (dictionaryForKey:). You use stub to allow your method to call a given method on a Mock Object but you don't need to verify that it was called.