Objective C - 单元测试和测试模拟对象?
- (BOOL)coolMethod:(NSString*)str
{
//do some stuff
Webservice *ws = [[WebService alloc] init];
NSString *result = [ws startSynchronous:url];
if ([result isEqual:@"Something"])
{
//More calculation
return YES;
}
return NO;
}
我正在使用 OCUnit 在下面的方法中,我如何模拟我的 WebService 对象,或者方法“startSynchronous”的结果以便能够编写独立的单元测试?
是否可以在其中注入一些代码来创建模拟 Web 服务或在 startSynchronous 调用时返回模拟数据?
- (BOOL)coolMethod:(NSString*)str
{
//do some stuff
Webservice *ws = [[WebService alloc] init];
NSString *result = [ws startSynchronous:url];
if ([result isEqual:@"Something"])
{
//More calculation
return YES;
}
return NO;
}
I am using OCUnit
In the following method how can i mock my WebService Object, or the result to the method "startSynchronous" to be able to write an independent unit test?
Is it possible to inject some code in there to either create a mock web service or return a mock data on startSynchronous call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用您想要的类别和重写方法,您甚至可以重写 init 方法以返回模拟对象:
这样做的问题是,如果您想让对象在 1 个测试文件中的不同测试中返回不同的结果,您不能这样做。 (您可以在每个测试页面重写每个方法一次)
编辑:
这是我发布的一个老问题,我想我会更新我现在如何编写可测试代码并对其进行单元测试的答案:)
ViewController Code
测试代码
One way is to use categories and override methods you want, you can even override the init method to return a mock object:
The problem with this is that if you want to make the object return different results in different tests in 1 test file you cannot do that. (You can override each method once per test page)
EDIT:
This is an old question I posted, I thought I would update the answer to how I write testable code and unit test it nowadays :)
ViewController Code
Test Code
构建在 aryaxt 的 WebService 答案之上,这里有一个小技巧,可以在不同的测试中获得不同的结果。
首先,您需要一个单例对象,用于在测试之前存储所需的答案
TestConfiguration.h
TestConfiguration.m
然后,您将定义“Mock”类别来定义模拟方法,例如:
最后,在测试本身中,您将混合模拟设置中的方法,并在调用
MyServiceTest.m
之前定义每个测试中的预期答案备注:在我的示例中,TestConfiguration 使用类/选择器作为键而不是对象/选择器。这意味着该类的每个对象都将使用相同的选择器答案。这很可能是你的情况,因为网络服务通常是单例的。但它应该改进为对象/选择器,可能使用对象的内存地址而不是它的类
Building on top of the WebService answer from aryaxt, here's a little trick to be able to get different results in different test.
First, you need a singleton object which will be used to store the desired answer, right before the test
TestConfiguration.h
TestConfiguration.m
Then you would define your "Mock" category to define mock methods , such as :
And finally, in the tests themselves, you would swizzle the mock method in the setup , and define the expected answer in each test, before the call
MyServiceTest.m
Remark : in my example, the TestConfiguration uses class/selector as a key instead of object/selector. That means every object of the class will use the same answer for the selector. That is most likely your case, as webservice are often singleton. But it should be improved to an object/selector maybe using the objet's memory address instead of its class