EF Code-first SetInitializer 不会在 NUnit TextFixture 设置中重新创建数据库
我在 NUnit Text Fixture 的开头为我的 EF 代码优先模型调用以下内容。
[TestFixtureSetUp]
public void Init()
{
DbDatabase.SetInitializer(
new MyDbInitialiser());
...
}
MyDbInitialiser
继承自 DropCreateDatabaseAlways
为什么 NUnit 测试装置只运行一次初始化程序,即使我重新运行整个测试装置?
如果我关闭 NUnit 运行程序并重新运行测试,数据库会删除并重新创建。
I call the following for my EF code-first model at the beginning of my NUnit Text Fixture.
[TestFixtureSetUp]
public void Init()
{
DbDatabase.SetInitializer(
new MyDbInitialiser());
...
}
MyDbInitialiser
inherits from DropCreateDatabaseAlways<MyDatabaseContext>
Why does the NUnit test fixture run the initializer only once even when I re-run the whole fixture of tests?
If I close the NUnit runner and re-run the tests, the database does drop and get re-created.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不熟悉 EF,但是有没有一种方法可以在 TestFixtureTearDown 中调用来在运行测试后进行清理?这可能保证当您第二次运行测试时,它将再次运行所有设置(而不是将某些内容保留在缓存中)
I'm not familiar with EF, but is there a method that you could call in a TestFixtureTearDown to clean up after running the tests? This might guarantee that when you run the tests a second time it will run all of the setup again (as opposed to keeping something in cache)
数据库类具有删除、创建、初始化等方法。我认为你可以自己打电话给他们来实现你所追求的目标。
The class Database has methods like Delete, Create, Initialize. I think you can call them yourself to achieve what you're after.
尝试改用
[Setup]
属性。任何标有此属性的方法都将在每个[Test]
之前调用。http://www.nunit.org/index.php?p =设置&r=2.2.10
Try using the
[Setup]
attribute instead. Any method marked with this attribute will be called before each[Test]
.http://www.nunit.org/index.php?p=setup&r=2.2.10