tSQLt ExpectException 对我不起作用

tSQLt ExpectException doesn't work for me

我有一个调用存储过程的测试。我希望存储过程使用 raiserror 拒绝提供的输入。 我的 tSQLt 测试的相关部分是:

EXEC tSQLt.ExpectException @ExpectedMessagePattern = '%more than one subcategory%';
EXEC usp_add_rfx_rfx lots, of, parameters, here

tSQLt 的测试结果取决于 usp_add_rfx_rfx sp 中 raiserror 的严重性。

如果严重性为 12 或更高,则测试失败并显示此消息:

[test_RFX_configuration].[test_multiple_categories_and_lots] failed: (Error) Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 1, current count = 0.[16,2]{test_multiple_categories_and_lots,0} (There was also a ROLLBACK ERROR --> The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION.{Private_RunTest,149})

如果严重程度较低,则测试失败如下:

[test_RFX_configuration].[test_multiple_categories_and_lots] failed: (Failure) Expected an error to be raised.

我用谷歌搜索了这个问题并尝试了很多我发现的东西——使用 TRY-CATCH 等,但没有成功。有什么想法吗?

该错误看起来像是正在测试的存储过程 (ups_add_rfx_rfx) 有它自己的内置事务处理。堆栈溢出也有类似的问题 How to ROLLBACK a transaction when testing using tSQLt and on that question Sebastien, one of tSQLt's authors links to one of his own blog posts on one approach to address that issue how to rollback in procedures