作者:手机用户2602906305_849 | 来源:互联网 | 2023-09-15 08:03
HowdoyouunittestObjectiveCcode?(iPhone)你如何单元测试ObjectiveC代码?(苹果手机)Inotherlanguagessuc
How do you unit test Objective C code? (iPhone)
你如何单元测试Objective C代码? (苹果手机)
In other languages such as java and .Net you can use Dependency Injection, to be able to unit test and pass mocked object in your current code. However, I could not find a reliable dependency injection framework for objective c.
在其他语言(如java和.Net)中,您可以使用依赖注入,以便能够在当前代码中进行单元测试并传递模拟对象。但是,我无法为目标c找到可靠的依赖注入框架。
Let's say you want to write a unit test for the code below, how can you mock MyObject?
假设你想为下面的代码编写一个单元测试,你怎么能模拟MyObject?
- (void) methodToBeTested
{
NSString str = @"myString";
MyObject object = [[MyObject alloc] init];
[object setString:str];
[object doStuff];
[object release];
}
This is how I would do it with dependency injection. Is there a similar way to achieve this on objective c?
这就是我如何通过依赖注入来做到这一点。是否有类似的方法在目标c上实现这一目标?
@Inject MyObject object;
public void methodToBeTested()
{
String str = "myString";
// object is automatically instantiated (Dependency Injection)
object.setString(str);
object.doStuff();
}
3 个解决方案