Code Monkey home page Code Monkey logo

Comments (8)

allending avatar allending commented on July 20, 2024

Interesting. Could you post a minimal sample project? Yes, I'm saying this because I am lazy.

from kiwi.

joshuatbrown avatar joshuatbrown commented on July 20, 2024

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.

allending avatar allending commented on July 20, 2024

Out of curiosity Josh, what did you do?

from kiwi.

joshuatbrown avatar joshuatbrown commented on July 20, 2024

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.

joshuatbrown avatar joshuatbrown commented on July 20, 2024

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.

sjmadsen avatar sjmadsen commented on July 20, 2024

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.

allending avatar allending commented on July 20, 2024

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.

allending avatar allending commented on July 20, 2024

Closing.

from kiwi.

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.