Comments (5)
I think this could be done via an option argument on the objection_register_singleton macro.
Something like
objection_register_singleton(class, targetClass)
objection_register_singleton(class, targetProtocol)
So whenever an instance of a targetClass or targetProtocol an instance of the class is created. If multiple class register then either an option can be added to the macro to decide which one used or last in best dressed could be used.
from objection.
I've used Objection's modules to accomplish this, (documented here) and it's worked out pretty well. In an integration test where I want to substitute some testing classes I'll create an injector like this:
JSObjectionInjector *injector = [JSObjection createInjector:[TestingObjectionModule new]];
And the TestingObjectionModule would have a configure
method something like:
- (void)configure {
[self bindBlock:^(JSObjectionInjector *context) {
return [FakeClock new];
} toClass:[Clock class]];
}
Now, that being said... it would be convenient to be able to change the configuration of an injector after it's been created, but before an instance of a particular class has been requested. Something more lightweight than having to create a new Objection module.
from objection.
Something more lightweight than having to create a new Objection module.
This is exactly what I did. I wrote a category for JSObjectionInjector
which updates a given module (basically just updates the injector context). Then you can create a module with public methods that calls bindBlock:toClass:
and bind any object you want (test doubles, for example).
In our code this is pretty transparent to the developer writing tests, since every spec is initializing an injector with this module (and basic modules we configure specifically for testing), and we have macros such as id mockedClass = MockClass([MyObject class]);
that create a new mock for class MyObject
, set it as the instance that is returned from the injector and returns it back to the user. Then you can perform all your expectations on it and verify them, since this instance is going to be returned from the injector on getObject:
.
from objection.
Ahh, I was wondering if modules could somehow do this. It wasn't clear to me from reading the documentation. Might be a good idea to add an example showing how this can work in a test situation as I think some people (like myself) will be interested.
Thanks.
from objection.
@StatusReport yeah +1 to @drekka's suggestion, it would be great if you could add this as an example.
from objection.
Related Issues (20)
- Support setting properties of non-object types
- Does objection inject dependencies automatically on creation? HOT 2
- frida ๅฝไปค้่ฏฏ
- Compatibility with Swift HOT 3
- Reseting Objection's global context storage caches HOT 2
- Injection at Initialization HOT 3
- Inject property declared in a category.
- Sharing singleton scope when deriving a new injector HOT 1
- @objection_initializer not passing arguments HOT 2
- Injecting object with protocol and superclass
- about sample? HOT 1
- why not use modern objective-c
- Why "JSObjectionModule.m" file line fifth to write "#import <Objection/JSObjectionProviderEntry.h>", but not "#import "JSObjectionProviderEntry.h"".
- why JSObjectionEntry's injector property is "assign" not "weak"?
- Passing nil arguments for get objection object HOT 7
- inScope:JSObjectionScopeSingleton doesn't work since Cocoapods version 1.6 HOT 19
- Build is broken on XCode 6.4 HOT 4
- Getting a reference to the JSObjectionInjector which created an object? HOT 2
- Undeclared selector warnings
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 objection.