How to mock location service using KIF-framework

n0_quarter

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.

vikingosegundo

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:

enter image description here

Probably the best: no code needs to be changed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to acknowledge system alerts automatically using KIF test framework?

From Dev

How to check/test value of a UISwitch using KIF?

From Dev

How to test for an empty table using KIF?

From Dev

How to mock a tornado coroutine function using mock framework for unit testing?

From Dev

How to implement mock location?

From Dev

How to implement mock location?

From Dev

How to Mock Service Initialization Parameters in Service Fabric for Unit Testing similar to mocking the state using a mock class?

From Dev

Jersey - How to mock a service

From Dev

Jersey - How to mock a service

From Dev

How can I change the default timeout using KIF?

From Dev

Double tap using KIF

From Dev

In Java, how can I mock a service loaded using ServiceLoader?

From Dev

How to test a Jersey rest service using mock objects

From Dev

How do I mock an Angular service using jasmine?

From Dev

How to test a Jersey rest service using mock objects

From Dev

How can I mock an Angular service using Protractor?

From Dev

How to mock an interface parameter for a service method using Spock and Grails 2?

From Dev

How to make an app to have location service option "while using the app"?

From Dev

How get user location using iOS geolocation framework

From Dev

Android Error testing app using Mock Location

From Dev

How to compare TextFrom UI and Text Which is received from Web Service in KIF 3.1.1?

From Dev

When testing an Angular service, how would I mock a controller using that service?

From Dev

How to use OCMock to mock a service

From Dev

How to mock an interface for entity framework?

From Dev

cordova ionic framework : get location background service

From Dev

AngularJS $http mock respond(...): how to mock a `location` header in the response?

From Dev

How to correctly and consistently mock location in Android?

From Dev

How to check for Mock Location in Android Marshmallow?

From Dev

How to correctly and consistently mock location in Android?

Related Related

  1. 1

    How to acknowledge system alerts automatically using KIF test framework?

  2. 2

    How to check/test value of a UISwitch using KIF?

  3. 3

    How to test for an empty table using KIF?

  4. 4

    How to mock a tornado coroutine function using mock framework for unit testing?

  5. 5

    How to implement mock location?

  6. 6

    How to implement mock location?

  7. 7

    How to Mock Service Initialization Parameters in Service Fabric for Unit Testing similar to mocking the state using a mock class?

  8. 8

    Jersey - How to mock a service

  9. 9

    Jersey - How to mock a service

  10. 10

    How can I change the default timeout using KIF?

  11. 11

    Double tap using KIF

  12. 12

    In Java, how can I mock a service loaded using ServiceLoader?

  13. 13

    How to test a Jersey rest service using mock objects

  14. 14

    How do I mock an Angular service using jasmine?

  15. 15

    How to test a Jersey rest service using mock objects

  16. 16

    How can I mock an Angular service using Protractor?

  17. 17

    How to mock an interface parameter for a service method using Spock and Grails 2?

  18. 18

    How to make an app to have location service option "while using the app"?

  19. 19

    How get user location using iOS geolocation framework

  20. 20

    Android Error testing app using Mock Location

  21. 21

    How to compare TextFrom UI and Text Which is received from Web Service in KIF 3.1.1?

  22. 22

    When testing an Angular service, how would I mock a controller using that service?

  23. 23

    How to use OCMock to mock a service

  24. 24

    How to mock an interface for entity framework?

  25. 25

    cordova ionic framework : get location background service

  26. 26

    AngularJS $http mock respond(...): how to mock a `location` header in the response?

  27. 27

    How to correctly and consistently mock location in Android?

  28. 28

    How to check for Mock Location in Android Marshmallow?

  29. 29

    How to correctly and consistently mock location in Android?

HotTag

Archive