Simon
Simon

Reputation: 25983

How can I access a core data store from an Xcode UI test?

We've got a suite of UI tests for our app written using KIF which I'd like to convert to use the new Xcode UI test framework.

Some of our current tests work like this:

Assert that there are no objects in a core data table
Do some stuff in the UI
Assert that there are some objects in the core data table
Cancel the thing we were doing in the UI
Assert that there are no leaked objects in the core data table

I need to look at the actual core data store, not just the UI, because the leaked rows wouldn't necessarily be shown in the UI. How can I access a core data store from an Xcode UI test?

Upvotes: 2

Views: 1287

Answers (2)

Aaron Sofaer
Aaron Sofaer

Reputation: 726

The answer is that you can't, not without abusing signals. XCUITests are not intended to touch the metal; they are intended to exercise user facing behavior only.

The test that you describe sounds like a perfectly good candidate for a unit test, though!

UPDATE:

(based on comments from OP)

Well, as far as I can tell you have four options

  1. you can create a backchannel that will use signals passing to break the separation between XCUITest and the app's internals.
  2. you can build functionality to mock the UI interactions in your unit tests so that you can validate against side effects.
  3. you can add an assertion and then exercise it manually.
  4. you can file a Radar asking for the functionality.

Upvotes: 4

Marcus S. Zarra
Marcus S. Zarra

Reputation: 46718

You can easily test against Core Data but your test you described does not make much sense. If you are cancelling the UI action then Core Data does not save to disk. When you mention "table" that means to me that you are looking on disk.

If you want to load an empty Core Data store, create some objects, verify they were created, delete them and confirm they were deleted; that can all be done in a UI test.

  1. Start with no store on disk (or use an in-memory store)
  2. Run your UI test
  3. Do a fetch against Core Data and confirm objects are in memory
  4. Perform your cancel code
  5. Confirm code is removed from Core Data.

What part are you having an issue with?

Upvotes: 0

Related Questions