Comments (5)
Same here! Any news ?
from ocmock.
Still don't have access to a Mac with Apple Silicon.
from ocmock.
I've looked a little at this and it seems like NSInvocation
is used. I then skimmed the documentation and found
I have no experience with NSInvocation
but I am wondering if those if those two sections are fulfilled for OCMock
's implementation or if OCMock
just doesn't support variable arguments for the time being?
from ocmock.
The best workaround I have is to get OCPartialMock to ignore (or "demock") specific selectors.
When mocking an object, OCM adds all the methods of the real object to call objc_msgForward. This allows OCM to handle the stubs. but because _objc_msgForward wraps the message into an NSInvocation, it will fail for variadic args.
The thing I do is redirect specific methods on the mockObject back to the original implementation of the real object. so that any calls to the variadic argument method is calling the actual method, rather than forwarding using an NSInvocation. While it means there is no test mocking for that method 😔, it also means that things won't crash 😃
I have written a category on OCPartialMockObject to do this and a macro "OCMIgnore" for convenience:
and added it to the top of my XCTTest Class.
#define OCMIgnore(object,method) do { if (object_getClass(object) == OCPartialMockObject.class) [(OCPartialMockObject*)object _ocm_ignoreMethod:method];} while (0)
@interface OCPartialMockObject : NSProxy @end // unimplemented declaration so the category implementation doesn't fail
@implementation OCPartialMockObject(SC)
-(void)_ocm_ignoreMethod:(SEL)methodSelector{
Ivar ivar = class_getInstanceVariable(OCPartialMockObject.class,"realObject");
if (ivar){
id mockObject = object_getIvar(self, ivar); // despite ivar name, the value is the created Mock object
if (mockObject){
Method mockMethod = class_getInstanceMethod(object_getClass(mockObject), methodSelector);
Method realMethod = class_getInstanceMethod(self.class, methodSelector);
if (realMethod && mockMethod){
IMP realIMP = method_getImplementation(realMethod);
IMP mockIMP = method_getImplementation(mockMethod);
if (mockIMP && realIMP && mockIMP == _objc_msgForward){
method_setImplementation(mockMethod, realIMP);
}
}
}
}
}
@end
Usage:
@interface MyObject : NSObject
-(void)log:(NSString*)format , ...;
@end
// Partial mock a new object
MYObject * mockObject = OCMPartialMock([MyObject.alloc init]);
// ignore (undo the mock) of the following selector:
OCMIgnore(mockObject,@selector(log:));
BTW this can be used for any method, not just ones that have variadic arguments.
from ocmock.
Actually in further use it is more complicated than this because stubbing new methods will rewire implementation pointers after an ignore :(
Wince my comment yesterday, I reworked things so that you can ignore mocking a specific selector for a class (and its descendent classes).
OCMIgnore([MyClass class],@selector(log:));
now registers the log: selector for the class as to be ignored. When mocking, stubbing and message forwarding, OCM mock will lookup the selector to by forwarded and ensure it is not forwarded in the mocking process. an that the real object gets the message:
The changes are multifold but can be found in pull request #539
from ocmock.
Related Issues (20)
- Documentation link is dead / goes to porn HOT 1
- Unable to access class methods +(void) while I am write down test case for my own framework -(void) instance methods are working fine using OCMock HOT 2
- OCMock not loading for macOS test target
- OCMStub an instance mthod not work as expected HOT 4
- When I OCMProtocolMock a protocol(This protocol has a method),How can I invoke the real implementation of the method HOT 4
- Mac Catalyst Support HOT 1
- Incompatible function pointer types HOT 2
- A question about the andDo function
- Developer Account agnostic build scripts HOT 1
- Do not require xcpretty HOT 1
- ReadOnly Mock HOT 1
- OCMPartialMock initialized with NSConstantArray is crashing on Xcode 14.0b3 HOT 3
- OCMVerify with CMTime argument fails on m1 mac. HOT 2
- Error in partial mock documentation HOT 1
- Release 3.9.2 not on Cocoapods HOT 2
- Request to provide Privacy Manifest file for OCMOCK SDK HOT 1
- `OCMVerifier.h` is not part of the public watchOS interface
- Xcode 15.3 build error on Intel Macs
- .andThrow(anException) will not throw the provided anException
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ocmock.