Closed jcampbell05 closed 8 years ago
Hey James, could you clarify with some code examples?
No code examples :) I simply want to just be able to use test double in swift without importing all the BBD Spec stuff.
Just the test doubles nothing else.
Ohhh that's unfortunate. I can appreciate why you'd want to use the test doubles (especially fake_for
, it's so convenient and powerful).
Unfortunately it's a terrible idea, and in my experience it always ends in disaster.
Swift (as of 2.0 at least) seems to make assumptions about whether a method call will be statically or dynamically dispatched. By default, it may assume that it can be statically dispatched, especially if you appear to be in an all-swift context. If the method is clearly marked as @objc
, then it will try to determine the correct method implementation at runtime, just like in Objective-C.
The end result is that if you pass in a test double from Cedar to a Swift class, you're going to have a bad time. The swift code you compiled is going to assume that Objective-C object came from Swift, and it's not going to have the same binary interface, most likely resulting in EXC_BAD_ACCESS
or other unrecoverable signals.
I found this out the hard way on one of my personal projects that mixes and matches Swift with Objective C, along with Cedar fakes.
So, I suppose the question I should be asking is... what do you want to do with Cedar's test doubles in Swift? Is your goal to enable easier unit testing without needing to instantiate your test subject's dependencies?
Mainly that :) But of course I could just set up DI using a framework and creak hand crafted mocks which I am told is the favoured way of doing it anyways.
You may also be interested in a tool I wrote that automatically generates test doubles (as source code) from a Swift protocol declaration @jcampbell05. This should make it a bit less onerous to write test doubles, but it comes with tradeoffs (the files are checked in, you have to keep them up to date, etc.).
@tjarratt Looks great. Keeping them up to date shouldn't be an issue as we use https://github.com/jcampbell05/xcake/ to generate our project :) so we can use the hook it has to make sure everything is built :)
I'm going to close this issue for now, but if you have more questions, or if anything changes w.r.t. using Cedar's test doubles with Swift, please don't hesitate to re-open it.
Oh and thank you for using Cedar, @jcampbell05!
I much prefer the double syntax here. Is there a way of just using this part of Cedar ? and will it work with Swift ?