Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit Testing Code Pattern #412

Closed
kenmaz opened this issue Feb 13, 2013 · 3 comments
Closed

Unit Testing Code Pattern #412

kenmaz opened this issue Feb 13, 2013 · 3 comments

Comments

@kenmaz
Copy link

kenmaz commented Feb 13, 2013

http://www.cimgf.com/2012/05/15/unit-testing-with-core-data/
Above blog is described about unit testing with MagicalRecord.

It say the pattern of a common test code is as follows.

@implemetation FooTest

- (void)setUp {
    [MagicalRecord setupCoreDataStackWithInMemoryStore];
}
- (void)tearDown {
    [MagicalRecord cleanUp];
}
- (void)testA {
   Foo* foo = [Foo createEntity];
   [MagicalRecord save];
   STAssertEquals([Foo countOfEntities], 1, nil);
}
- (void)testB {
   Foo* foo = [Foo createEntity];
   [MagicalRecord save];
   STAssertEquals([Foo countOfEntities], 1, nil);  // FAIL: entities count is 2.
}

I think [MagicalRecord cleanUp] doesn't clear test data in InMemoryStore.
If testB is executed after testA, testB is failed.
I expect Foo entities count is always 1 in testB method.

In common case, we want to clear all data in each test method execution.
I think more common pattern of testing code is like here.

@implemetation FooTest

- (void)setUp {
    [MagicalRecord setupCoreDataStackWithInMemoryStore];
}
- (void)tearDown {
    [[NSPersistentStoreCoordinator defaultStoreCoordinator] removePersistentStore:[NSPersistentStore defaultPersistentStore] error:nil];  // clear all test data.
    [MagicalRecord cleanUp];
}
....

What do you think about my opinion?
Please give me advice.

@tonyarnold
Copy link
Contributor

Given the age of this issue, and the volume of issues we have to work through, I've decided to close this alongside a number of other older issues.

If you can still replicate the issue under the latest in-development version of MagicalRecord (3.0 at the time of writing), please feel free to re-open and one of @magicalpanda/team-magicalrecord will take another look. Thanks!

@ahknight
Copy link

I'm seeing this as well. It works for the first test, but after -cleanUp is run, the auto setup refuses to create the stack again (or at least the MOM, which leads to not seeing any entities).

- (void)setUp {
    [super setUp];

    // Put setup code here. This method is called before the invocation of each test method in the class.

    // Create the default testing context.  Note this is not using SQLite.
    [MagicalRecord setDefaultModelFromClass:[self class]];
    [MagicalRecord setupCoreDataStackWithInMemoryStore];
}

- (void)tearDown {
    [super tearDown];

    // Put teardown code here. This method is called after the invocation of each test method in the class.

    // Reset Core Data between tests.
    [MagicalRecord cleanUp];
}

@ahknight
Copy link

Removing the -setDefaultModelFromClass line gives me "cannot setup persistent store with nil model!" later on. Replacing that line with the following works:

    [NSManagedObjectModel MR_setDefaultManagedObjectModel:[NSManagedObjectModel MR_mergedObjectModelFromMainBundle]];

I suspect -cleanUp is nil-ing the model and the setup call isn't putting it back properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants