在 MsUnit 测试之间清理 SQLCompact DB 中的数据
我正在使用仅代码持久性来使用 MVC2、实体框架和 CTP4。我在 MSUnit 中为我的域对象创建了一些单元测试,其中一些测试是为了了解持久性在此范例中如何工作。我使用 Sql Server CE 4.0 进行这些测试。这工作得很好,除了一个问题......数据似乎在同一类的测试之间保持不变。
我以前有使用 Java、Hibernate Annotations 和 HSQLDB 的经验,在这种情况下,数据库会在每次测试执行时创建和拆除。然而,在 SqlCompact 中,我有几个测试使用相同的测试数据装置,如果我同时运行它们,最终会出现约束违规。
我可以通过一些技巧来解决这个问题,在 [TestCleanup]
中显式删除表/删除数据,但是在这种情况下使用 SQL Compact 时确保我从每个测试开始使用新的数据库的正确方法是什么?我确信答案很简单,但我似乎无法在任何地方找到它。谢谢。
编辑:目前,我正在这样做,这很有效,但我不喜欢它。欢迎更好的想法:
[TestCleanup]
public void teardown()
{
mgr.Database.DeleteIfExists();
mgr.Database.Create();
}
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:
[TestCleanup]
public void teardown()
{
mgr.Database.DeleteIfExists();
mgr.Database.Create();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为更好的方法是将拆卸中的代码添加到 [TestInitialize],该代码在每个测试执行之前被调用。将此与 [ClassInitialize] 进行比较,后者在整个固定装置中被调用一次。
我对 NUnit 更熟悉,发现此表有助于将 NUnit 属性映射到 MSUnit
http://blogs.msdn .com/b/nnaderi/archive/2007/02/01/mstest-vs-nunit-frameworks.aspx
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