使用 TSQLUnit 测试 INSTEAD OF 触发器

发布于 2024-08-31 01:13:43 字数 267 浏览 3 评论 0原文

我的 SQL Server 2005 数据库中的表上有一个 INSTEAD OF 触发器,用于检查多个传入值。如果传入值无效,则会引发错误并回滚事务。否则将插入记录。

我想包含此触发器的 TSQLUnit 测试,其中如果插入无效值,则回滚事务就是测试的成功结果。我创建了一个测试过程来执行此操作,但回滚事务会中止整套测试的执行。

有人在这方面取得过成功吗?如果是这样,你是如何实现的?

如果 TSQLUnit 无法做到这一点,那么如何测试触发器?或者你对它们进行了测试吗?

I have an INSTEAD OF trigger on a table in my SQL Server 2005 database that checks several incoming values. If an incoming value is invalid, an error is raised and the transaction is rolled back. Otherwise the record is inserted.

I would like to include a TSQLUnit test of this trigger where, if an invalid value is inserted, having the transaction rolled back is the successful outcome of the test. I have created a test procedure to do this, but rolling back the transaction aborts execution of the whole suite of tests.

Has anyone had success with this? If so, how did you accomplish it?

If this is not possible with TSQLUnit, how do you test your triggers? Or do you test them at all?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

披肩女神 2024-09-07 01:13:43

我不知道你看到的是 TSQLUnit 还是 SQL 的标准行为。

SQL Server 2000 及更早版本中的触发器异常是 ROLLBACK 时批量中止因为 @@TRANCOUNT = 0

使用 SQL Server 2005 中的 TRY/CATCH,行为会发生变化,客户端应该正确处理它。话虽如此,无论如何我也会将外部调用包装在 TRY/CATCH 中。

建议:

  • 检查前后数据的状态,看看您
  • 使用存储过程(这就是您正在做的事情)
  • 使用 TRY/CATCH

I don't know if it's TSQLUnit or just standard behavior of SQL you're seeing.

A trigger exception in SQL Server 2000 and earlier is batch aborting on ROLLBACK because @@TRANCOUNT = 0

With TRY/CATCH in SQL Server 2005 the behavior changes and the client should handle it correctly. Saying that, I'd wrap the outer call in TRY/CATCH too anyway.

Suggestions:

  • check the state of data before and after to see what you have
  • use a stored procedure (which is what you're doing anyway)
  • use TRY/CATCH
生生不灭 2024-09-07 01:13:43

还有另一个名为 TST http://tst.codeplex.com/ 的 TSQL 单元测试框架,据说允许测试代码有自己的交易。来自 TST 文档:

“当测试的代码有自己的事务时,TST 使用可靠的方式来检测其自己的回滚机制变得无效的情况。可以在测试、套件或全局级别禁用 TST 回滚。”

我没有使用过这个框架,但我在研究这个主题时遇到了它(以及你的问题)。

There is another TSQL unit testing framework called TST http://tst.codeplex.com/ that supposedly allows testing code that has its own transactions. From the TST docs:

"When the tested code has its own transactions, TST uses a reliable way of detecting the cases where its own rollback mechanism becomes ineffective. The TST rollback can be disabled at the test, suite or global level."

I have not used this framework, but I came across it (and your questions) while researching this topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文