Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XCTestCase with internet connection case

I'm writing an app that has separated functions when online and offline. In those function, I use Reachability to check internet connection and with each case (online/offline), it does different works.

Now, I've been required to write test cases for those business logics. I've searched everywhere, but it seems that no one care much about test case in iOS.

I'd like to write testcase that cover for both online/offline case. Does it possible in iOS? If so, how do I simulate the internet connection status?

UPDATE QUESTION:

I also want to cover the case switching from online to offline and vice versa. There should be a way to simulate this network connection status, right?

like image 672
Eddie Avatar asked Jan 25 '26 08:01

Eddie


1 Answers

You should mock return value using OCMock

id reachabilityMock = OCMClassMock([Reachability class]);
[OCMStub([reachabilityMock currentReachabilityStatus]) andReturnValue:@(ReachableViaWiFi)];
[OCMStub([reachabilityMock reachabilityForInternetConnection]) andReturn:reachabilityMock];
Reachability *reachability = [Reachability reachabilityForInternetConnection];
XCTAssertEqual(reachability.currentReachabilityStatus, ReachableViaWiFi);

Then send reachability changed notification manually.

[[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
like image 194
Bing Avatar answered Jan 27 '26 22:01

Bing



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!