cedarbdd / cedar

BDD-style testing using Objective-C
http://groups.google.com/group/cedar-discuss
1.19k stars 140 forks source link

Subspec just for Doubles #368

Closed jcampbell05 closed 8 years ago

jcampbell05 commented 8 years ago

I much prefer the double syntax here. Is there a way of just using this part of Cedar ? and will it work with Swift ?

akitchen commented 8 years ago

Hey James, could you clarify with some code examples?

jcampbell05 commented 8 years ago

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.

tjarratt commented 8 years ago

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?

jcampbell05 commented 8 years ago

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.

tjarratt commented 8 years ago

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.).

https://github.com/tjarratt/xcode-better-refactor-tools

jcampbell05 commented 8 years ago

@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 :)

tjarratt commented 8 years ago

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!