开发者

Cleaning up data within SQLCompact DB between MsUnit tests

I'm playing with MVC2, Entity Framework and CTP4 using code only persistence. I've created some unit tests in MSUnit for my domain objects, including some to see how persistence works in this paradigm. I'm using Sql Server CE 4.0 for these tests. This works fine, except for one problem...data seems to be persisted between tests within the same class.

I have previous experience using Java, Hibernate Annotations, and HSQLDB and in that case the DB is created and torn-down on each test execution. In SqlCompact, however, I have a couple tests that use the same test data fixture and end up with constraint violations if I run them both.

I can fix this via some hacks to drop tables/delete data explicitly within [TestCleanup] but what is the proper way to ensure that I start with a fresh DB for each test when using SQL Compact in this case? I'm sure the answer is simple, but I can't seem to find it anywhere. Thanks.

EDIT: For the moment, I'm doing this, which works--but I don't like it. Better ideas are welcome:

    [开发者_Go百科TestCleanup]
    public void teardown()
    {
        mgr.Database.DeleteIfExists();
        mgr.Database.Create();
    }


I think a better approach is to add the code you have in teardown to [TestInitialize], which gets called before each test executes. Compare this with [ClassInitialize] which gets called once for the entire fixture.

I am more familiar with NUnit, and found this table helpful to map NUnit attributes to MSUnit http://blogs.msdn.com/b/nnaderi/archive/2007/02/01/mstest-vs-nunit-frameworks.aspx

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜