Comments (8)
As I mentioned in the previous issue, there is no obvious way of doing this. Any partial stubbing of real objects requires swapping out the method implementation at run-time which just isn't possible to do safely with toll-free bridged objects.
I'm not sure I really understand the use-case for this though; as a general rule of thumb, you should aim to only ever stub and mock types you own, which would preclude the need to stub native Foundation objects. Why do you need this?
from kiwi.
Not feasible, if you were unlucky it might seem to work when you try.
Even stubbing and mocking Apple provided Objective-C types or types where you perform your own shenanigans with method implementations is a fragile exercise. The implementation that is there tries to be smart about it and should cover the basic cases, but simply cannot account for all the different scenarios where internal swizzling is taking place.
Like Luke, I'm curious as to when doing it would actually be desirable.
from kiwi.
Matt: if you'd like to explain why you feel you need this, I'd be happy to try and help you find a better way.
from kiwi.
Okay. Sorry for the late reply.
I would like to set expectations & stub methods on objects of class NSString, NSArray, etc. because:
Let's imagine I write a category on NSString to add the instance methods:
-[NSString words]
, which returns anNSArray
of the words in the receiver.-[NSString wordCount]
, which returns anNSUInteger
of the number of words in the receiver.
To spec wordCount
, I want to set an expectation that it should call words
on a string and return some array. Then, I want to set an expectation on the returned array that it should receive count
and return an NSUInteger wordCount
. Then, I want to confirm that when I call wordCount
, it returns the NSUInteger wordCount
above.
Also, RSpec lets me set expectations and stub methods on String & Array.
from kiwi.
I'm struggling to see why on earth you'd want to test like that. This isn't really the way to use mocks/stubs. Mocks are useful when you have objects playing clearly defined roles interacting with each other. They aren't so great for things like simple data transformation with no external dependencies like the above example.
It strikes me that a few simple input/output tests would be far more useful and less tightly coupled to your implementation.
I appreciate that RSpec let's you do these the with String and Array but that doesn't mean you should. "Only mock types you own" and "mock roles, not objects" are important rules to live by if you want to make good use of mocks.
from kiwi.
I read, "unit tests should test a method's external behavior rather than its internal implementation," on Wikipedia Mock Objects.
And the RSPec Book says, "Even the most highly decoupled code has some dependencies. Sometimes they are on objects that are cheap and easy to construct and have no complex state. These generally don’t present a problem, so there is no need to create stubs for them."
So, if I used input/output tests to test wordCount
, first, I guess I should make a factory or something because I'd be using the same input strings that I used to test the words
method. That's cool I guess. But, second, if the words
method has an error, the wordCount
test would also fail even if it's implemented correctly.
I thought a unit test was only supposed to test one unit/method at a time?
P.S. I'm ordering that book you recommended off Amazon... :)
from kiwi.
How should I stub NSDate
methods like -timeIntervalSinceNow
?
from kiwi.
I'm not sure should I open a new issue or better to ask here. So I will start with asking here)
I'm testing some network-related code and would like to stub statusCode
method of NSHTTPURLResponse. But thit method should return NSInteger, but if I stub such method with NSNumber value it will return NSNumber too and next test will fail:
it(@"should return NSInteger from stubbed method", ^{
NSHTTPURLResponse *mockedResponse = [NSHTTPURLResponse mock];
[mockedResponse stub:@selector(statusCode) andReturn:@200];
[[theValue(mockedResponse.statusCode) should] equal:theValue(200)];
});
Is there any way to stub method which are return for example NSInteger?
Thanks in advance.
from kiwi.
Related Issues (20)
- Use predicate in stubbed properties when filtering mock objects in array HOT 1
- Macros autocomplete not working HOT 1
- when the second run testCase, not real run and success directly
- Swift 3 cocoapods run failed HOT 3
- Tests are executed in a wrong order under certain conditions (Xcode 8.3, single suite)
- Please help me :D Invalid pointer dequeued from free list
- Using a sharedExample in another HOT 2
- kiwi report error that' SenTestingKit/SenTestingKit.h file not found' HOT 2
- build synthetic seismogram in a specific station HOT 1
- Stop tests execution if one test is failed HOT 1
- Xcode 9: `any` Macro Conflicts with Generated Swift Header Macro `SWIFT_MODULE_NAMESPACE_PUSH` HOT 6
- Preparing next release HOT 8
- theValue(BOOL) not working with should receive
- XCTest.framework/XCTest' does not contain bitcode HOT 2
- expectFutureValue not working HOT 2
- How to test cadisplaylink
- Oops… the system encountered a problem (#500) - Retrying in X:XXs… HOT 1
- SharedExample with multiple 'it' blocks
- SIGABRT: kiwi crashes at KWProbePoller
- Preparing a new version (3.1.0) - Upgrading to Xcode 11 HOT 4
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 kiwi.