ocmock

How to mock class method (+)? [duplicate]

孤街浪徒 提交于 2019-12-01 06:48:04
This question already has an answer here: Objective C & OC Mock - Mocking a class method? 4 answers Need to write unit testing for the following code, I want to do mock for class method canMakePayments, return yes or no, so far no good method found dues to canMakePayments is a class method (+), seems all OCMock methods are all used for instance method (-). You guys any suggestion or discussion will be appreciated. Thanks. // SKPaymentQueue.h // StoreKit if ([SKPaymentQueue canMakePayments]){ .... } else{ ... } Since you can't intercept the method by providing a different instance, what you can

XCTest fails when calling [NSBundle mainBundle]

佐手、 提交于 2019-12-01 06:43:29
问题 I have some code that calls [NSBundle mainBundle] at some point, mainly to read/set preferences. When I unit test the method, the test fails because the test's mainBundle does not contain the file. This is a known issue, that Apple won't fix as they consider it is not a bug: the gist of their reply is that XCTest is working correctly by returning it’s own main bundle instead of the bundle of the test target. Previously our codebase was using a category override on NSBundle 's +mainBundle

Can OCMock run a block parameter?

隐身守侯 提交于 2019-11-30 06:20:25
Assume a method signature such as the following: - (void)theMethod:(void(^)(BOOL completed))completionBlock; I would like to mock this method signature to ensure the method is called, and just call the completion block. I see from other posts like this one that I can mock the method call and accept any block, but not run the block. I also know there is a andDo method that I might be able to use, but I can't figure out how to pass a block in and run it. Any ideas? Thanks. You can use [[mock stub] andDo:] like this to pass another block that gets called when your mocked method is called: void (

How do you mock out private properties with OCMock for iOS?

北城余情 提交于 2019-11-30 04:51:29
I have a private property that is declared in the .m file of my class to be tested, let's call it ClassUnderTest. ClassUnderTest instantiates an instance of ClassToBeMocked. How do I use OCMock to mock out an instance of the ClassToBeMocked and assign it to the ClassUnderTest? Re-declare the property in your test class. You can do the same for private methods. In ClassUnderTestTest.m: @interface ClassUnderTest () @property(retain)ClassToBeMocked *instanceToBeMocked; -(void)somePrivateMethod; @end Does the following work? id classUnderTest = ... // get from somewhere id mock = [OCMockObject

Test target X encountered an error (Early unexpected exit, operation never finished bootstrapping - no restart will be attempted

余生颓废 提交于 2019-11-29 21:11:29
I have started working with OCMock to write test cases for the existing project that i have integrated in my project workspace. After following all the steps mentioned in this link When i first executed my test case its giving me this error. I searched it and tried following some of the solutions like "creating new target"," restarting Xcode" but it didn't help me out. Any Idea? I have my notes and demo applications for both Cocoapods and Carthage here https://github.com/onmyway133/TestTarget Make sure all frameworks are linked to the Test targets Configure Runpath Search Paths to point to $

How can I get OCMock under ARC to stop nilling an NSProxy subclass set using a weak property?

馋奶兔 提交于 2019-11-29 14:09:07
问题 Under ARC , I have an object, Child that has a weak property, parent . I'm trying to write some tests for Child , and I'm mocking its parent property using OCMock . Under ARC, setting an NSProxy subclass using a synthesized weak property setter doesn't set the property ... the line after the weak property is set, checking it reveals that it's already nil . Here's the concrete example: @interface Child : NSObject @property (nonatomic, weak) id <ParentInterface>parent; @end @implementation

Unit testing Parse framework iOS

≯℡__Kan透↙ 提交于 2019-11-29 08:01:59
I am attempting to write unit tests for an iOS application which makes use of the Parse backend framework, and after much experimentation seem to be failing at writing successful unit tests. I have found a few posts on testing asynchronous code ( Testing asynchronous call in unit test in iOS ) and testing network calls, but am yet to find a way of testing calls to the Parse backend with async callbacks. To give an example, could anyone advise how I would test the following line of code: [PFUser saveUser:userModelMock withBlock:^(BOOL success, NSError *error) { }]; I am using OCMock and the

Can OCMock run a block parameter?

大憨熊 提交于 2019-11-29 05:02:30
问题 Assume a method signature such as the following: - (void)theMethod:(void(^)(BOOL completed))completionBlock; I would like to mock this method signature to ensure the method is called, and just call the completion block. I see from other posts like this one that I can mock the method call and accept any block, but not run the block. I also know there is a andDo method that I might be able to use, but I can't figure out how to pass a block in and run it. Any ideas? Thanks. 回答1: You can use [

how to unit testing AFNetworking request

老子叫甜甜 提交于 2019-11-29 04:12:34
i am making a GET request to retrieve JSON data with AFNetworking as this code below : NSURL *url = [NSURL URLWithString:K_THINKERBELL_SERVER_URL]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; Account *ac = [[Account alloc]init]; NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET" path:[NSString stringWithFormat:@"/user/%@/event/%@",ac.uid,eventID] parameters:nil]; AFHTTPRequestOperation *operation = [httpClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) { NSError *error = nil; NSDictionary

how to unit test a NSURLConnection Delegate?

Deadly 提交于 2019-11-29 02:55:05
问题 How can I unit test my NSURLConnection delegate? I made a ConnectionDelegate class which conforms to different protocols to serve data from the web to different ViewControllers. Before I get too far I want to start writing my unit tests. But I don't know how to test them as a unit without the internet connection. I would like also what I should do to treat the asynchronous callbacks. 回答1: This is similar to Jon's response, couldn't fit it into a comment, though. The first step is to make sure