Comments (21)
vote+1
from ocmockito.
+1 from me as well.
from ocmockito.
Noted, and keeping this issue open.
from ocmockito.
+1 for me too
from ocmockito.
+1 for me too
from ocmockito.
+1
from ocmockito.
+2! (I'm pairing - they agree ;) )
from ocmockito.
+3 since we have 3 ppl using them
from ocmockito.
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.
I'd say accepting not nil, not null matchers and ignoring it on verify otherwise
from ocmockito.
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.
@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.
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.
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.
+1
from ocmockito.
@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.
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.
I also would really welcome any implementation of this behavior. Is there anything coming up?
from ocmockito.
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.
Give it a try! See ArgumentCaptorTest.m for examples.
from ocmockito.
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)
- Swift Static Declaration of 'times' follows non-static declaration HOT 1
- Expected nil, but was nil HOT 3
- Crash when calling mocks from `dealloc` that are not called from elsewhere HOT 16
- MKTArgumentCaptor to HCArgumentCaptor migration HOT 2
- verify macro replaces argument type with id HOT 2
- Unable to @import OCMockitoIOS HOT 4
- Is there a way to assert method throws? HOT 1
- Stubbing the protocol return as Nil HOT 3
- Cannot use OCMockito because of OCHamcrest. HOT 2
- Feature request: possibility to call through HOT 1
- Argument captor capturing outer function HOT 1
- Import OCHamcrest as a module breaks backward compatibility and feels unnecessary HOT 1
- Mock internal calls of a another class methods HOT 4
- Feature Request: Apple Silicon binary release HOT 9
- Catalyst Support for OCMockito.xcframework HOT 8
- verify doesn't fail when parameter is mocked HOT 11
- Stubs return nil when verifying HOT 3
- Could not build OCMockito using Carthage HOT 3
- Building with Carthage fails HOT 1
- Use OCHamcrest 9.0.1 via all dependency managers
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 ocmockito.