Skip to content

Loading…

Unit Testing Code Pattern #412

Closed
kenmaz opened this Issue · 3 comments

3 participants

@kenmaz

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

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!

@tonyarnold tonyarnold closed this
@ahknight

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

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
Something went wrong with that request. Please try again.