简体   繁体   English

OCMockito 存根单例共享实例方法

[英]OCMockito stub singleton shared instance method

I was using OCMock for stubbing class lvl methods and it worked well.我正在使用 OCMock 对类 lvl 方法进行存根,并且效果很好。 Now I need to achieve the same behaviour with OCMockito.现在我需要使用 OCMockito 实现相同的行为。

In OCMock I have following:在OCMock中,我有以下几点:

_mock = mockClass([MySingleton class]);
OCMStub([_mock sharedSingleton]).andReturn(_mock);

In my case I have to stub sharedSingleton method because it is used in multiple places during tests.在我的情况下,我必须存根 sharedSingleton 方法,因为它在测试期间在多个地方使用。

OCMock's ability to directly stub singletons is done through method swizzling. OCMock 直接存根单例的能力是通过方法 swizzling 完成的。 For OCMockito, I purposely avoid this approach.对于 OCMockito,我故意避免这种方法。 I recommend you use some form of Dependency Injection to provide the instance.我建议您使用某种形式的依赖注入来提供实例。

If you don't want that, you can always intermix OCMock and OCMockito.如果您不希望那样,您可以随时混合使用 OCMock 和 OCMockito。 That way you can continue to have the OCMock behavior, while using OCMockito for other things.这样你就可以继续拥有 OCMock 行为,同时将 OCMockito 用于其他事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM