Giter VIP home page Giter VIP logo

Comments (21)

iksnae avatar iksnae commented on September 28, 2024

vote+1

from ocmockito.

cruffenach avatar cruffenach commented on September 28, 2024

+1 from me as well.

from ocmockito.

jonreid avatar jonreid commented on September 28, 2024

Noted, and keeping this issue open.

from ocmockito.

formvoltron avatar formvoltron commented on September 28, 2024

+1 for me too

from ocmockito.

ronak2121 avatar ronak2121 commented on September 28, 2024

+1 for me too

from ocmockito.

mjbeauregard avatar mjbeauregard commented on September 28, 2024

+1

from ocmockito.

daveshah avatar daveshah commented on September 28, 2024

+2! (I'm pairing - they agree ;) )

from ocmockito.

xr1337 avatar xr1337 commented on September 28, 2024

+3 since we have 3 ppl using them

from ocmockito.

jonreid avatar jonreid commented on September 28, 2024

OK, lots of interest in this! I assume the main need is the ability to mock a method that takes block arguments. So here's my question: What would you verify against a given block argument? Or would you want OCMockito to silently ignore it, checking the other arguments?

from ocmockito.

ronak2121 avatar ronak2121 commented on September 28, 2024

I'd say accepting not nil, not null matchers and ignoring it on verify otherwise

from ocmockito.

mjbeauregard avatar mjbeauregard commented on September 28, 2024

At the time I added my vote, I was trying to test a network sync class which in turn uses a REST client class (subclass of AFHTTPClient). I wanted to mock the REST client class so that I could simulate various network responses, both failures and successful results. The REST client consists mainly of async networking API methods that use a success block to callback with results and a failure callback with HTTP error codes, etc

I was trying to use OCMockito to mock the REST client and set it up to respond with specific success results or failure codes to ensure correct functionality of the sync class. Ultimately I ended up mocking the client by subclassing and overriding the various async methods. The tests would give the mock client a mock response object and the overridden methods would immediately (synchronously) call one of the two blocks with the prescribed response values.

This actually worked really well and was quite minimal in terms of code, but maybe there is a more elegant way to do this more generally if OCMockito were extended in some way.

from ocmockito.

jonreid avatar jonreid commented on September 28, 2024

@metatation, that's a great example of when not to use a mocking framework like OCMockito. Subclass and Override Method is a powerful tool — don't let the existence of mocking frameworks fool you into thinking they can do better than you!

from ocmockito.

slevin avatar slevin commented on September 28, 2024

Thanks for that last answer @jonreid, I've been using mocks primarily for that use case, not seeing the easily implemented solution right in front of my face. Thanks!

from ocmockito.

codyaray avatar codyaray commented on September 28, 2024

Personally I'd like to capture the block arguments so that I can call them and verify they work as expected based on the parameters given. :)

from ocmockito.

brow avatar brow commented on September 28, 2024

+1

from ocmockito.

crispinb avatar crispinb commented on September 28, 2024

@codyaray: that's exactly what I need too. I find myself having to hand-roll quite a few mocks to capture block arguments.

from ocmockito.

grantjk avatar grantjk commented on September 28, 2024

I like the idea of being able to capture block based arguments. Most APIs are heading this way now and the easier it is to mock / stub these, the more likely they'll be tested. As @metatation mentioned, this is quite popular in network calls. Capturing blocks also allows you to do synchronous testing.

While you can subclass and override, this is such a common occurrence that you'd end up subclassing most of the classes in your project.

My two cents. I've also included an example from Kiwi below.


Kiwi has an option for stub:withBlock: which allows you to capture blocks passed to a method and then you can call them directly with whatever params you want in your test. This creates a really easy way to stub block based apis.

For example, if you have an object with a method refresh

- (void)refresh
{
  [self.fetcher fetchAllWithCompletion:^(NSError* error){
     // do stuff
  }];
}

The goal being that in your test case you can essentially do the following:

- (void)testRefreshShowsAnAlertWhenErrorOccurs
{

   // set up other stuff
....

// stub the block api and call the block synchronously
     [fetcherMock stub:@selector(fetchAllWithCompletion:) withBlock:^ id (NSArray *params) {
                __block void(^block)(NSError *) = params[0];
                block([NSError nullMock]);
                return nil;
            }];


// call the method under test
    [object refresh];

// verify behaviour
    [verfiy(alertMock) show];
}

I haven't used OCMock before, but I assume [[mock stub] andDo:theBlock] offers similar functionality.

from ocmockito.

tspecht avatar tspecht commented on September 28, 2024

I also would really welcome any implementation of this behavior. Is there anything coming up?

from ocmockito.

jonreid avatar jonreid commented on September 28, 2024

I've started work on a way to capture arguments. Then you can get your blocks back and invoke them as you like.

from ocmockito.

jonreid avatar jonreid commented on September 28, 2024

Give it a try! See ArgumentCaptorTest.m for examples.

from ocmockito.

ronak2121 avatar ronak2121 commented on September 28, 2024

Thanks a lot on adding this! Is the pod spec for this project going to update? Which version of the library had this support?

from ocmockito.

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.