How to mock location service using KIF-framework - ios

I use KIF framework (http://github.com/kif-framework/KIF) for UI Tests
and I need to mock location service.
The problem is location service starts BEFORE KIF method -beforeAll invoked.
So it's too late to mock.
Any suggestions would be appreciated.

In my KIF target I have a BaseKIFSearchTestCase : KIFTestCase, where I overwrite CLLocationManager`s startUpdatingLocation in a category.
Note that this is the only category overwrite I ever made as this is really not a good idea in general. but in a test target I can accept it.
#import <CoreLocation/CoreLocation.h>
#ifdef TARGET_IPHONE_SIMULATOR
#interface CLLocationManager (Simulator)
#end
#implementation CLLocationManager (Simulator)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
-(void)startUpdatingLocation
{
CLLocation *fakeLocation = [[CLLocation alloc] initWithLatitude:41.0096334 longitude:28.9651646];
[self.delegate locationManager:self didUpdateLocations:#[fakeLocation]];
}
#pragma clang diagnostic pop
#end
#endif // TARGET_IPHONE_SIMULATOR
#import "BaseKIFSearchTestCase.h"
#interface BaseKIFSearchTestCase ()
#end
#implementation BaseKIFSearchTestCase
//...
#end
Cleaner would be to have a subclass of CLLocationManager in your application target and another subclass with the same name in your test target that send fake location like shown above. But if this is possible depends on how your test target is set up, as it actually need to be an application target as Calabash uses it.
Yet another way:
in your project create another configuration "Testing", cloning "Debug"
add the Preprocessor Macro TESTING=1 to that configuration.
Subclass CLLocationManager
use that subclass where you would use CLLocaltionManger
conditionally compile that class
#import "GELocationManager.h"
#implementation GELocationManager
-(void)startUpdatingLocation
{
#if TESTING==1
#warning Testmode
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
CLLocation *fakeLocation = [[CLLocation alloc] initWithLatitude:41.0096334 longitude:28.9651646];
[self.delegate locationManager:self didUpdateLocations:#[fakeLocation]];
});
#else
[super startUpdatingLocation];
#endif
}
#end
in your test targets scheme choose the new configuration
And yet another option:
Probably the best: no code needs to be changed.

As usual, a couple of ways to do this. The key is not to try to mock out the existing location service but to have a completely different mock you can get access to at run time. The first method I'm going to describe is basically building your own tiny DI container. The second method is for getting at singletons you don't normally have access to.
1) Refactor your code so that it doesn't use LocationService directly. Instead, encapsulate it in a holder (could be a simple singleton class). Then, make your holder test-aware. The way this is works is you have something like a LocationServiceHolder that has:
// Do some init for your self.realService and make this holder
// a real singleton.
+ (LocationService*) locationService {
return useMock ? self.mockService : self.realService;
}
- (void)useMock:(BOOL)useMock {
self.useMock = useMock;
}
- (void)setMock:(LocationService*)mockService {
self.mockService = mockService;
}
Then whenever you need your locationService you call
[[LocationServiceHolder sharedService] locationService];
So that when you're testing, you can do something like:
- (void)beforeAll {
id mock = OCClassMock([LocationService class]);
[[LocationServiceHolder sharedService] useMock:YES]];
[[LocationServiceHolder sharedService] setMock:mock]];
}
- (void)afterAll {
[[LocationServiceHolder sharedService] useMock:NO]];
[[LocationServiceHolder sharedService] setMock:nil]];
}
You can of course do this in beforeEach and rewrite the semantics to be a bit better than the base version I'm showing here.
2) If you are using a third party LocationService that's a singleton that you can't modify, it's slightly more tricky but still doable. The trick here is to use a category to override the existing singleton methods and expose the mock rather than the normal singleton. The trick within a trick is to be able to send the message back on to the original singleton if the mock doesn't exist.
So let's say you have a singleton called ThirdPartyService. Here's MockThirdPartyService.h:
static ThirdPartyService *mockThirdPartyService;
#interface ThirdPartyService (Testing)
+ (id)sharedInstance;
+ (void)setSharedInstance:(ThirdPartyService*)instance;
+ (id)mockInstance;
#end
And here is MockThirdPartyService.m:
#import "MockThirdPartyService.h"
#import "NSObject+SupersequentImplementation.h"
// Stubbing out ThirdPartyService singleton
#implementation ThirdPartyService (Testing)
+(id)sharedInstance {
if ([self mockInstance] != nil) {
return [self mockInstance];
}
// What the hell is going on here? See http://www.cocoawithlove.com/2008/03/supersequent-implementation.html
IMP superSequentImp = [self getImplementationOf:_cmd after:impOfCallingMethod(self, _cmd)];
id result = ((id(*)(id, SEL))superSequentImp)(self, _cmd);
return result;
}
+ (void)setSharedInstance:(ThirdPartyService *)instance {
mockThirdPartyService = instance;
}
+ (id)mockInstance {
return mockThirdPartyService;
}
#end
To use, you would do something like:
#include "MockThirdPartyService.h"
...
id mock = OCClassMock([ThirdPartyService class]);
[ThirdPartyService setSharedInstance:mock];
// set up your mock and do your testing here
// Once you're done, clean up.
[ThirdPartyService setSharedInstance:nil];
// Now your singleton is no longer mocked and additional tests that
// don't depend on mock behavior can continue running.
See link for supersequent implementation details. Mad props to Matt Gallagher for the original idea. I can also send you the files if you need.
Conclusion: DI is a good thing. People complain about having to refactor and having to change your code just to test but testing is probably the most important part of quality software dev and DI + ApplicationContext makes things so much easier. We use Typhoon framework but even rolling your own and adopting the DI + ApplicationContext pattern is very much worth it if you're doing any level of testing.

Related

Unit testing private methods from a category?

I have a category on NSString class that contains a private helper method.
It would be handy if I could use this method in my unit test.
However I have difficulties to expose it.
When I create a class extension on NSString and declare the method here, the method is not visible in unit test. And it doesn't matter if I create the class extension in a separate header file, or as a part of unit test .m file.
It looks like I am missing something here.
Any help guys?
Common unit testing guidance would tell you not to try and test your private methods. Only test via your public interfaces. Private methods are simply an implementation detail that could change at any time, when you refactor. Your public interfaces should be pretty stable, and will exercise your private methods.
However, if you still want to test your private category methods, the following works for me...
First, your category:
UIImage+Example.h
#interface UIImage (Example)
#end
UIImage+Example.m
#implementation UIImage (Example)
+ (NSString *)examplePrivateMethod
{
return #"Testing";
}
#end
MyExampleTests.m
#import <XCTest/XCTest.h>
#import "UIImage+Example.h"
#interface UIImage (Example_Test)
+ (NSString *)examplePrivateMethod;
#end
#interface MyExampleTests : XCTestCase
#end
#implementation MyExampleTests
- (void)testExample
{
XCTAssertEqualObjects(#"Test", [UIImage examplePrivateMethod], #"Test should be test");
}
#end
Essentially, redeclare your private method in a new category in your test. However, as mentioned above this is exposing private methods just for the purpose of testing, and coupling your tests to your implementation.
You can execute any method (private or not) on an object by simply using performSelector: on it, like so:
[something performSelector:#selector(somePrivateMethod)];
But I agree with James that you should only do that when absolutely necessary.

How to forbid to inherit init method?

I have been struggling to find a correct and explicit answer, so I have decided to ask it here.
I create class A and define init method there:
#interface A : NSObject
- (id)initWithHealth:(int)hp;
#end
Then I am creating class B, that inherits from class A and define another init method there:
#interface B : A
- (id)initWithHealth:(int)hp andDamage:(int)dmg;
#end
In main, when I am going to instantiate an object from class B, I will be suggested by Xcode to use either - (id)initWithHealth:(int)hp; OR - (id)initWithHealth:(int)hp andDamage:(int)dmg; init method.
How can I forbid for class B to inherit init method from class A? I want my class B to have only one init method that I define. Is there a way to achieve this?
Thanks in advance.
You have a few choices:
Option 1 - supply a default "damage" with the old init method. In class B you would add:
- (id)initWithHealth:(int)hp {
return [self initWithHealth:hp andDamage:0]; // use an appropriate default
}
- (id)initWithHealth:(int)hp andDamage:(int)dmg {
self = [super initWithHealth:hp];
if (self) {
// do stuff with dmg
return self;
}
Option 2 - cause a runtime error if the old init method is used. In class B you would add:
- (id)initWithHealth:(int)hp {
NSAssert(0, #"Dont use this");
return nil; // make compiler happy
}
Personally I think Option 1 is the better choice.
In your subclass B you can explicitly define the method such that the class will not respond to it at runtime.
- (instancetype) initWithHealth:(int) hp {
[self release];
[super doesNotRecognizeSelector:_cmd];
return nil;
}
This is very conventional, I've seen it used on a lot of open source projects and on some of my client's projects. I think this is preferable to #rmaddy's solution when you want to ensure the inherited method is NEVER called. If you want a sane default when the inherited init method is called, then the other code works just fine.
EDITED (thanks to Sulthan and rmaddy)
OK I have found the answer to my question, I should have used categories.
But thanks for everyone who participated.

How to send not declared selector without performSelector:?

Background:
I have an object (let's call it BackendClient) that represents connection with server. Its methods are generated to single #protocol and they are all synchronous, so I want to create proxy object that will call them in background. The main problem is return value, which I obviously can't return from async method, so I need to pass a callback. The "easy" way will be copy all BackendClient's methods and add callback argument. But that's not very dynamic way of solving that problem, while ObjectiveC nature is dynamic. That's where performSelector: appears. It solves problem entirely, but it almost kills proxy object transparency.
Problem:
I want to be able to send not declared selector to proxy (subclass of NSProxy) object as if it was already declared.
For example, I have method:
-(AuthResponse)authByRequest:(AuthRequest*)request
in BackendClient protocol. And I want proxy call look like this:
[proxyClient authByRequest:myRequest withCallback:myCallback];
But this wouldn't compile because
No visible #interface for 'BackendClientProxy' declares the selector 'authByRequest:withCallBack:'
OK. Let's calm down compiler a bit:
[(id)proxyClient authByRequest:myRequest withCallback:myCallback];
Awww. Another error:
No known instance method for selector 'authByRequest:withCallBack:'
The only thing that comes to my mind and this point is somehow construct new #protocol with needed methods at runtime, but I have no idea how to do that.
Conclusion: I need to suppress this compilation error. Any idea how to do that?
If I understand it, you have a synchronous, non-threaded, API that you want to be asynchronous for purposes of not blocking, say, the main event loop, etc...
I would add a serial queue to BackgroundClient:
#property(strong) dispatch_queue_t serialQueue;
... somewhere in your -init ...
_serialQueue = dispatch_queue_create(..., serial constant);
Then:
- (void)dispatchOperation:(dispatch_block_t)anOperation
{
dispatch_async(_serialQueue, anOperation);
}
That can be used like:
[myClient dispatchOperation:^{
[myClient doSynchronousA];
id result = [myClient doSynchronousB];
dispatch_async(dispatch_get_main_queue(), ^{
[someone updateUIWithResult:result];
}
}];
That is the easiest way to move the BackgroundClient to an asynchronous model without rewriting it or heavily refactoring it.
If you want to harden the API, then create a class wrapper for BackendClient that holds an instance of the client and the serial queue. Make it such that said class instantiates the client and the rest of your code only retrieves instances from that wrapper. That'll allow you to still have the same dispatchOperation: model, but not require mirroring all the methods.
typedef void(^ AsyncBackendBlock(BackendClient* bc);
#interface AsyncBackend
+(instancetype)asyncBackendWithBackend:(BackendClient*)bc;
#property .... serialQueue;
- (void) dispatchAsync:(AsyncBackendBlock) backBlock;
#end
.m:
#interface AsyncBackend()
#property... BackendClient *client;
#end
#implementation AsyncBackend
- (void) dispatchAsync:(AsyncBackendBlock) backBlock
{
dispatch_async(_serialQueue, ^{
backBlock(_client);
});
}
#end
Caller:
AsyncBackend *b = [AsyncBackend asyncBackendWithBackend:[BackendClient new]];
[b dispatchAsync:^(BackendClient *bc) {
[bc doSomething];
id result = [bc retrieveSomething];
dispatch_async(dispatch_get_main_queue(), ^{
[uiThingy updateWithResult:result];
}
}];
....
To look up a selector at runtime, you can use NSSelectorFromString(), but in this case you should just go ahead and import whatever header you need to get the declaration of -authByRequest:

ObjectiveC: Need suggestion for my way of having protected method? [duplicate]

This question already has answers here:
Protected methods in Objective-C
(9 answers)
Closed 9 years ago.
Simply put, I need a way to have some private methods in a class that are only exposed for its subclasses, and it is difficult (maybe impossible) to do this in Objective-C.
What I did so far:
// MyClass.h
#protocol MyClassProtectedMethodsProtocol
- (void)__protectedMethod;
#end
#interface MyClass : NSObject
- (void)publicMethod;
- (id<MyClassProtectedMethodsProtocol>)protectedInstanceForSubclass:(id)subclass;
#end
Then:
// MyClass.m
#import "MyClass.h"
#interface MyClass() <MyClassProtectedMethodsProtocol>
#end
#implementation MyClass
- (void)publicMethod
{
// something
}
- (id<MyClassProtectedMethodsProtocol>)protectedInstanceForSubclass:(id)subclass
{
if ([subclass isKindOf:MyClass.class] && ![NSStringFromClass(subclass.class) isEqualToString:NSStringFromClass(MyClass.class)])
{
// the subclass instance is a kind of MyClass
// but it has different class name, thus we know it is a subclass of MyClass
return self;
}
return nil;
}
- (void)__protectedMethod
// something protected
{
}
#end
Then the subclass of MyClass can just:
id<MyClassProtectedMethodsProtocol> protectedMethodInstance = [self protectedMethodForSubclass:self];
if (protectedMethodInstance != nil)
{
[protectedMethodInstance protectedMethod];
}
This way does not break OO (compared to calling the private method and ignoring the compiler warning, or even guessing the private method name as only .h is known), but a protocol is needed for the available protected methods and once this is exposed, in a big project that we only deliver interface and static library to client, client can actually know the private methods and try to call them regardless of warning. And the bigest problem is from outside of the subclass, user can as well call this method to get the protectedInstance. Can anyone advice?
Thanks
Check this: Protected methods in Objective-C
Simply put, there is no way to prevent a method from being called in Objective-C, since ultimately, the client can still call performSelector on any object.
A standard way to handle this scenario is to include the internal methods in a separate header, like MySuperClass_Internal.h. Use a class extension: #interface MySuperClass (Internal). Do not install MySuperClass_Internal.h at /usr/local/include or in the framework, or however you're delivering the library to your clients.

how do I make an objective-c delegate for a cpp class?

I'm stuck trying to combine openGL-es (xcode openGL game template with the ogles2tools library from powervr 3.0 sdk. My problem is the line of code where I load the effect file:
/*
Load the effect.
We pass 'this' as an argument as we wish to receive callbacks as the PFX is loaded.
This is optional and supplying NULL implies that the developer will take care
of all texture loading and binding to to the Effect instead.
*/
if(m_pEffect->Load(*m_pEffectParser, "Effect", c_szPfxFile, NULL, uiUnknownUniforms, &error) != PVR_SUCCESS)
{
NSLog(#"%s",error.c_str());
return;
}
I'm supposed to pass a "this" pointer so I can receive the callbacks. The delegate method I need to implement is:
EPVRTError OGLES2IntroducingPFX::PVRTPFXOnLoadTexture(const CPVRTStringHash& TextureName, GLuint& uiHandle, unsigned int& uiFlags)
{
/*
This is an optional callback function for PVRTPFXEffect and can be used to automate
the texture loading process.
If multiple effects are to be loaded and they share textures it would be
prudent to have a caching system in place so texture memory is not wasted.
Please see OGLES2MagicLantern for an example of this.
*/
if(PVRTTextureLoadFromPVR(TextureName.String().c_str(), &uiHandle) != PVR_SUCCESS)
return PVR_FAIL;
return PVR_SUCCESS;
}
I guess the big issue for me is how do I go about providing a cpp delegate method in objective-c? I did some reading on this issue, but it seemed what I was reading was going the other way. That is, an objective-c delegate in cpp. It's pretty confusing, but here's my thought...
I create a cpp class the implements the method I need. I add that to my viewController class and pass the pointer to this cpp class in the m_pEffect->Load call. Does this seem correct?
Thanks.
P.S. Sorry if my code formatting is bad. I'm still learning.
Edit: Here's the example I found regarding mixing the objective-c and cpp. It seems really similar to what I want to do.
Update: Here's some additional info (requested by user1118321)
The CPP class which needs a delegate is CPVRTPFXEffect (PVRTPFXParserAPI.h - from powerVR SDK 3.0). I would add a link, but I'm not sure if this is allowed. Here's a link to the class header, but this version (and others on the web) did not include the pDelegate attribute for the load method. I'm assuming they are examples of a previous version. Let me know if it's okay to post this class file and I will do so.
I found a good example of what I think I'm supposed to do from reading this thread. So here's what I have so far:
My CPP delegate class...
class myCppDelegate : public PVRTPFXEffectDelegate {
public:
myCppDelegate() {};
EPVRTError PVRTPFXOnLoadTexture(const CPVRTStringHash& TextureName, GLuint& uiHandle, unsigned int& uiFlags) {
return PVR_FAIL;
};
};
My Obj-C wrapper class (just modified from the example link above)...
struct RNWrapOpaque;
#interface RNWrap : NSObject {
struct RNWrapOpaque *_cpp;
}
- (id)init;
#end
implementation...
#import "RNWrap.h"
#import "Wrap.h"
#interface RNWrap ()
#property (nonatomic, readwrite, assign) RNWrapOpaque *cpp;
#end
#implementation RNWrap
#synthesize cpp = _cpp;
struct RNWrapOpaque
{
public:
RNWrapOpaque() : wrap() {};
myCppDelegate wrap;
};
- (id)init
{
self = [super init];
if (self != nil)
{
self.cpp = new RNWrapOpaque();
}
return self;
}
- (void)dealloc
{
delete _cpp;
_cpp = NULL;
// [super dealloc];
}
#end
Basically I am able to compile the code and debug, but when the the CPVRTPFEffect class makes this call:
if(pDelegate->PVRTPFXOnLoadTexture(pTexDesc->FileName, uiHandle, uiFlags) != PVR_SUCCESS)
I get EXC_BAD_ACCESS. I'm assuming it's not finding my callback method, because I set a breakpoint and the line never gets called.
Here's my updated code which calls CPVRTPFXEffect::Load using a bridge command for the delegate parameter.
if(m_pEffect->Load(*m_pEffectParser, "Effect", c_szPfxFile,(__bridge myCppDelegate*)opaqueCppWrap, uiUnknownUniforms, &error) != PVR_SUCCESS)
Thanks for your help!
Update 2: The project uses ARC. Here's what my viewController interface looks like:
#interface ViewController : GLKViewController {
...
RNWrap* opaqueCppWrap;
...
}
#property (strong) RNWrap *opaqueCppWrap;
Adding the #property didn't help with the EXC_BAD_ACCESS. I'm not sure how to "see" the value of pDelegate when I'm tracing the CPP code. Xcode doesn't reveal anything when I hover over the variable.
I added the following line of code to the CPVRTPFXEffect::Load method (just prior to the line where it crashes):
*pReturnError += PVRTStringFromFormattedStr("Here is your class typeid: %s.\n", typeid(pDelegate).name());
return PVR_FAIL;
This is what displayed in the debug output window:
Here is your class typeid: P21PVRTPFXEffectDelegate.
I'm not sure what the "P21" means (if anything), but it looks like I'm close to getting this working. I dunno, maybe this is as close as it gets. Still crashing and not finding my method.
First, you may want to look at the last article in the series on wrapping C++. Most of it has gotten much simpler in the latest versions of clang. You probably don't need half this code anymore. ObjC++ objects can now have private C++ properties without any tricks, while maintaining a pure-ObjC interface.
Here is how you want to think about this problem:
Build a C++ object that is the delegate. Write all the code involved in setting up the delegation, etc, in C++. So when it says "pass a this pointer" you should really be passing a this pointer (because you should be doing this in the C++ code). The fact that you're doing a _bridge cast in a C++ call is a real hint something is going wrong.
Let an ObjC own the C++ object as a property.
Write the delegate callbacks in C++ inside the C++ object. If useful, you can let the C++ object then make calls into the ObjC object as needed, but it may be easier if the C++ object does all the delegate work.
I finally got this working, but had to remove the obj-c wrapper class from my viewController in order to do so. Here's what the code looks like:
ViewController.h
struct Opaque;
#interface ViewController : GLKViewController {
...
//RNWrap* opaqueCppWrap; // this didn't work
struct Opaque *opaqueCpp; // try this
...
}
ViewController.mm
// declare the Opaque structure
struct Opaque {
public:
Opaque() : cppobject() {};
myCppDelegate cppobject;
};
viewDidLoad
// ... create opaque member on initialization
opaqueCpp = new Opaque();
//opaqueCppWrap = [[RNWrap alloc] init]; // old way of doing things using wrapper
pass the delegate to the Load method
// old way using bridge cast and wrapper
//if(m_pEffect->Load(*m_pEffectParser, "Effect", c_szPfxFile,(__bridge myCppDelegate*)opaqueCppWrap, uiUnknownUniforms, &error) != PVR_SUCCESS)
// this works...
if(m_pEffect->Load(*m_pEffectParser, "Effect", c_szPfxFile, (myCppDelegate*)opaqueCpp, uiUnknownUniforms, &error) != PVR_SUCCESS)
Not sure why the wrapper class doesn't work, but I'm happy that my callback is working (appy no crashy!)
Phew, that was rough. Any thoughts/comments?

Resources