Git Product home page Git Product logo

Comments (5)

drekka avatar drekka commented on August 23, 2024

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.

baconpat avatar baconpat commented on August 23, 2024

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.

StatusReport avatar StatusReport commented on August 23, 2024

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.

drekka avatar drekka commented on August 23, 2024

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.

glentregoning avatar glentregoning commented on August 23, 2024

@StatusReport yeah +1 to @drekka's suggestion, it would be great if you could add this as an example.

from objection.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.