cedarbdd / cedar

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

Can XCTestExpectation be used in conjunction with cedar? #364

Closed fatuhoku closed 8 years ago

fatuhoku commented 8 years ago

I don't intend to use Cedar's assertion syntax, but was curious whether tests defined in Cedar are scoped in a way that XCTestExpectation tests still run and are understood as they should be?

Cheers.

tjarratt commented 8 years ago

Hey @fatuhoku, that's a great question. Cedar takes some pain to play nicely with XCTest, but we haven't actually allowed users to use XCTestExpectation. I believe you would need to have a reference to the underlying XCTestCase, so you could invoke waitForExpectationsWithTimeout:handler:. That is not something you could do with Cedar today.

It would be hard / strange for Cedar to provide you an XCTestCase instance, because Cedar also supports running tests without XCTest at all. We could do this, but historically Cedar has taken the approach of not providing async assertions by default. While it can be tempting to write tests for async code, it's often easier (and better in my opinion) in the long run to write tests that are synchronous. These tests end up being simpler, easier to reason about when they fail, and easier to maintain in the long run.

I should note, however, there is a project called Cedar-Async which adds some asynchronous support to Cedar. I haven't used it extensively, but the API looks pretty similar to XCTestExpectation. I don't think it's being actively maintained, but it fulfills a pretty similar role and plays nicely with Cedar.

fatuhoku commented 8 years ago

@tjarratt Thanks for the response. My use-case is one of testing UIKit (e.g. test that a view controller is pushed after a button press). I found that it was necessary to yield to UIKit by using dispatch_after. The test actually runs almost instantly, but it is an async test.

I'll think about checking out the async library then.

briancroom commented 8 years ago

Hey @fatuhoku, you may be interested in checking out PivotalCoreKit to reduce the need for asynchronous tests when working with UIKit. It includes a number of spec helpers that stub out select pieces of UIKit API to allow them to be tested synchronously.

If you do take that approach, be sure to complement your unit tests with some acceptance tests using something like KIF or XCTest UI testing, to give you test coverage in an unstubbed environment as well.