[TestMethod]public voID Foo{ using (var ts = new TransactionScope()) { // Arrange SetupMyTest(context); // Act PerformMyTest(context); var numberOfChanges = context.SaveChanges(SaveOptions.AcceptAllChangesAfterSave); // if there's an issue,chances are that an exception has been thrown by Now. // Assert Assert.IsTrue(numberOfChanges > 0,"Failed to _____"); // transaction will rollback because we do not ever call Complete on it }}解决方法 我们使用TransactionScope.
private TransactionScope transScope; #region Additional test attributes // // Use Testinitialize to run code before running each test [Testinitialize()] public voID MyTestinitialize() { transScope = new TransactionScope(); } // Use TestCleanup to run code after each test has run [TestCleanup()] public voID MyTestCleanup() { transScope.dispose(); }
这将回滚在任何测试中所做的任何更改.
总结以上是内存溢出为你收集整理的c# – 如何在EF4中回滚单元测试TearDown?全部内容,希望文章能够帮你解决c# – 如何在EF4中回滚单元测试TearDown?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)