ocmock

OCMock 3.0.2 linker error with .mm test file

人走茶凉 提交于 2019-12-05 14:54:07
I am using OCMock 3.0.2, which I've installed through cocoapods for my test target: platform :ios, '7.0' xcodeproj 'myProject.xcodeproj' target :myTestTarget do pod 'OCMock', '~> 3.0.2' end link_with "myTestTarget" In my test file (myTest.mm), I've included OCMock and want to try out the new verify-in-place strategy, like so: - (void) test_myTest { MyObject *obj = [MyObject new]; id robotMock = OCMPartialMock(obj); [obj testMethod]; // some asserts OCMVerify([obj _internalMethodToBeCalled]); } So far seems normal. However, when I tried to run this specific test case, I am getting linker error:

OCMock an NSOperation

前提是你 提交于 2019-12-05 12:56:51
I am trying to write some Unit Tests to test some custom NSOperations that we are writing. What I'd like to do is create a Mock of the NSOperation and put it on the NSOperationQueue , and then wait for it to finish. I know I can swizzle the methods and not use OCMoc k at all, but I really don't want to do that. I'd like to use OCMock . The code I'm trying to run is something like the following: MYOperation *operation = [MYOperation new]; id mockOperation = [OCMockObject partialMockForObject:operation]; [NSOperationQueue *queue = [NSOperationQueue new]; [queue setMaxConcurrentOperationCount:1];

stub method with block of code as parameter with OCMock

狂风中的少年 提交于 2019-12-05 10:36:10
Is there a way to stub method, that takes block as it's parameter? For example mehod: - (void)reverseGeocodeLocation:(CLLocation *)location completionHandler:(CLGeocodeCompletionHandler)completionHandler; Yes. The easiest way would be to accept anything: id mockGeocoder = [OCMockObject mockForClass:[CLGeocoder class]]; [[mockGeocoder stub] reverseGeocodeLocation:[OCMOCK_ANY] completionHandler:[OCMOCK_ANY]]; It gets a bit trickier if you want to verify a particular block is passed in. One option is to make your completion handler a property of your class, initialize it when you initialize your

How to I capture an argument sent to a mock?

旧巷老猫 提交于 2019-12-05 09:12:14
Does anyone know how to capture an argument sent to an OCMock object? id mock = [OCMockObject mockForClass:someClass] NSObject* captureThisArgument; [[mock expect] foo:<captureThisArgument>] [mock foo:someThing] GHAssertEquals[captured, someThing, nil]; How do I go about validating the argument to foo? I'm happy to do it within a block in the mock definition too, but if I could get the object out so that I can assert on feature of it later that would be brilliant. Is this possible with OCMock? Quentin If you want to validate your parameter maybe you can do it directly while you are setting

OCMock - verifying order of method calls. Is there any other way?

时光毁灭记忆、已成空白 提交于 2019-12-05 07:27:38
When I want to verify that in one method a mock object is receiving some messages in a particular order I do something like this: // sut is an instance of the class I am testing and myMock is a mock object injected in sut. // I want to test that myMock sends messageA and then messageB, in that particular order. [[[myMock expect] andDo:^(NSInvocation *invocation) { [[myMock expect] messageB]; }] messageA]; [sut methodToTest]; [myMock verify]; Is there any cleaner/better way of doing this? Thanks in advance. You can use setExpectationOrderMatters [myMock setExpectationOrderMatters:YES]; [[myMock

Testing controller method with OCMock and Core Data

筅森魡賤 提交于 2019-12-05 01:20:29
问题 I am just grasping the concepts of TDD and mocking, and am running into an issue in terms of how to properly. I have a sheet that drops down and lets a user create a new core data object and save it to the data store. I am not sure if I am taking the best approach to testing it. - (IBAction)add:(id)sender { NSString *itemName = [self.itemNameTextField stringValue]; SGItem *newItem = [NSEntityDescription insertNewObjectForEntityForName:kItemEntityName inManagedObjectContext:[self

Passing primitives to an OCMock's stub

余生长醉 提交于 2019-12-04 16:15:25
问题 I'm learning how to use OCMock to test my iPhone's project and I have this scenario: a HeightMap class with a getHeightAtX:andY: method, and a Render class using HeightMap . I'm trying to unit test Render using some HeightMap mocks. This works: id mock = [OCMockObject mockForClass:[Chunk class]]; int h = 0; [[[mock stub] andReturnValue:OCMOCK_VALUE(h)] getHeightAtX:0 andY:0]; Of course, works only for x=0 and y=0 . I want to test using a "flat" height map. This means I need to do something

How do i mock a method that accepts a handle as an argument in OCMock?

爱⌒轻易说出口 提交于 2019-12-04 15:57:52
问题 I'm trying to mock a method that has the equivalent of the following signature: - (NSDictionary *) uploadValues:(BOOL)doSomething error:(NSError **)error I want it to return a small dictionary so that my test can make sure the code uses the dictionary properly. however, no matter what i do OCMock always returns nil from the method, regardless of how i stub it out. The error begins as nil in the code i'm testing, and these are the different ways i've tried stubbing it: NSError * error = nil; [

How to use Core Data for Dependency Injection

佐手、 提交于 2019-12-04 10:47:30
I'm toying with using Core Data to manage a graph of objects, mainly for dependency injection (a subset of the NSManagedObjects do need to be persisted, but that isn't the focus of my question). When running unit tests, I want to take over creation of the NSManagedObjects, replacing them with mocks. I do have a candidate means of doing this for now, which is to use the runtime's method_exchangeImplementations to exchange [NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:] with my own implementation (ie. returning mocks). This works for a small test I've done. I have

How can I use OCMock to verify that a method is never called?

拈花ヽ惹草 提交于 2019-12-04 09:53:32
问题 At my day job I've been spoiled with Mockito's never() verification, which can confirm that a mock method is never called. Is there some way to accomplish the same thing using Objective-C and OCMock? I've been using the code below, which works but it feels like a hack. I'm hoping there's a better way... - (void)testSomeMethodIsNeverCalled { id mock = [OCMockObject mockForClass:[MyObject class]]; [[[mock stub] andCall:@selector(fail) onObject:self] forbiddenMethod]; // more test things here,