简体   繁体   English

在MsUnit测试之间清理SQLCompact DB中的数据

[英]Cleaning up data within SQLCompact DB between MsUnit tests

I'm playing with MVC2, Entity Framework and CTP4 using code only persistence. 我正在使用仅代码持久性的MVC2,实体框架和CTP4。 I've created some unit tests in MSUnit for my domain objects, including some to see how persistence works in this paradigm. 我已经在MSUnit中为我的域对象创建了一些单元测试,其中包括一些用于查看持久性在此范例中如何工作的单元测试。 I'm using Sql Server CE 4.0 for these tests. 我正在使用Sql Server CE 4.0进行这些测试。 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. 我以前有使用Java,Hibernate注释和HSQLDB的经验,在这种情况下,将创建数据库并在每次测试执行时将其拆除。 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. 但是,在SqlCompact中,我有两个测试使用相同的测试数据夹具,并且如果我同时运行它们,则会遇到约束违规的情况。

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? 我可以通过一些技巧来解决此问题,以在[TestCleanup]显式删除表/删除数据,但是在这种情况下使用SQL Compact时,确保我为每个测试从一个新的DB开始的正确方法是什么? 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: 欢迎提出更好的主意:

    [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. 我认为一种更好的方法是将您在拆解中拥有的代码添加到[TestInitialize]中,该代码在每次测试执行之前都会被调用。 Compare this with [ClassInitialize] which gets called once for the entire fixture. 将此与[ClassInitialize]进行比较,该类将为整个灯具调用一次。

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 我对NUnit更加熟悉,发现该表有助于将NUnit属性映射到MSUnit http://blogs.msdn.com/b/nnaderi/archive/2007/02/01/mstest-vs-nunit-frameworks.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM