Comments (8)
Interesting. Could you post a minimal sample project? Yes, I'm saying this because I am lazy.
from kiwi.
I've actually found a better way to write the code I was trying to test, so I no longer need or care about being able to stub methods in the AppDelegate. I'll leave it up to you to decide if it's something you want to implement or not - you can close the issue if you like.
from kiwi.
Out of curiosity Josh, what did you do?
from kiwi.
The method I was trying to stub in the AppDelegate was actually -managedObjectContext
(not -bar
, surprisingly :)), and since my controllers were getting the managedObjectContext from the AppDelegate, I thought it would be easy enough to stub that method to return a test managedObjectContext. Instead of the controllers asking for the managedObjectContext, I've changed the implementation of them to use dependency injection - that way, I can just inject the test managedObjectContext into my controllers when I initialize them in my tests.
from kiwi.
Is this failing because I'm trying to stub an instance method on a class object? I had a similar issue today where I was trying to spy on an instance method with a class object. So it looked like this:
[[MyClass should] receive:@selector(initWithFoo:) withArguments:foo];
where initWithFoo:
is (obviously?) an instance method. But I don't have an instance of MyClass that I can spy/stub until after initWithFoo:
executes.
Does Kiwi support spying and stubbing like this?
from kiwi.
I was trying to do something very similar today. I stubbed +[NSDate date]
to return a fixed point in time, but one part of my implementation invoked -timeIntervalSinceNow
on an instance of NSDate
. (I worked around it by replacing that call with -timeIntervalSinceDate:[NSDate date]
.)
It is feasible within the constraints of Objective-C to do something like [[NSDate anyInstance] stub:@selector(timeIntervalSinceNow) andReturn:theValue(1)]
?
from kiwi.
Probably not a good idea. I have no idea what the implementation behind NSDate is. Kiwi replaces method implementations on objects dynamically. If the implementation of the class does the same or something incompatible ........
Generally you want to stub methods on classes you own/created. In this instance, I am curious to learn why you are stubbing [NSDate date]
. The work around you mentioned sounds cleaner, granted I don't all the context.
Regarding the last part of your question, yes it is generally possible (exceptions being when the implementation of a class has some trickery going on). In fact replacing a method implementation for all instances of a class is far less involved that replacing the implementation of a single instance. See class_replaceMethod(...) as a starting point if you are interested.
Whether you would want to ever do this in anything other than some sort of lower level library is another matter.
from kiwi.
Closing.
from kiwi.
Related Issues (20)
- Use predicate in stubbed properties when filtering mock objects in array HOT 1
- Macros autocomplete not working HOT 1
- when the second run testCase, not real run and success directly
- Swift 3 cocoapods run failed HOT 3
- Tests are executed in a wrong order under certain conditions (Xcode 8.3, single suite)
- Please help me :D Invalid pointer dequeued from free list
- Using a sharedExample in another HOT 2
- kiwi report error that' SenTestingKit/SenTestingKit.h file not found' HOT 2
- build synthetic seismogram in a specific station HOT 1
- Stop tests execution if one test is failed HOT 1
- Xcode 9: `any` Macro Conflicts with Generated Swift Header Macro `SWIFT_MODULE_NAMESPACE_PUSH` HOT 6
- Preparing next release HOT 8
- theValue(BOOL) not working with should receive
- XCTest.framework/XCTest' does not contain bitcode HOT 2
- expectFutureValue not working HOT 2
- How to test cadisplaylink
- Oops… the system encountered a problem (#500) - Retrying in X:XXs… HOT 1
- SharedExample with multiple 'it' blocks
- SIGABRT: kiwi crashes at KWProbePoller
- Preparing a new version (3.1.0) - Upgrading to Xcode 11 HOT 4
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 kiwi.