Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

setupCoreDataStackWithInMemoryStore writes to the default Core Data Stack #567

Closed
penaeric opened this Issue Sep 8, 2013 · 1 comment

Comments

Projects
None yet
2 participants

penaeric commented Sep 8, 2013

I'm trying to create some XCTests that involve Core Data. I follow the ideas described on this post (Unit Testing with Core Data).

By using the following lines of code, the test should use an in-memory store to perform tests against a Core Data store, but instead it writes to the default Core Data store (the one that the main app uses).

I created a very simple sample test project that shows this behavior: https://github.com/penaeric/CoreData-XCTest

The app has two entities: Person and Organization. The first time you run it it'll create 2 Organizations and 3 Persons. This will happen only once.

The XCTest has two simple tests. In the setup method I use the following code to setup a in-memory store:

    [MagicalRecord setDefaultModelFromClass:[self class]];
    [MagicalRecord setupCoreDataStackWithInMemoryStore];

You can run the full test suite. Some times I received a Can't locate entity {entity name} error, I haven't figure out why, as it doesn't always happen. Either way, you should be able to run the tests separately.

The setup method creates an Organization and 6 Persons. You can run the testExample test and then run the app. You'll see at least 9 Persons (3 from the initial run + 6 from the test).

The second test, makes sure that only 6 Person objects are in the database, but it fails because there are more.
On my tearDown method I call MR's cleanUp, but it looks like it's not cleaning up either the main store or the in-memory store.

Contributor

tonyarnold commented Apr 8, 2014

This is a logic error in your tests. Your application inserts debug entities in viewWillAppear: — 3 of them — so the result the test is seeing (9 entities) is correct.

@tonyarnold tonyarnold closed this Apr 8, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment