简体   繁体   English

tSQLt - 处理多个SQL Server结果集

[英]tSQLt - Handling multiple SQL Server result sets

I'm creating a SQL Server unit test using tSQLt. 我正在使用tSQLt创建SQL Server单元测试。

The proc that I'm testing returns 3 result sets. 我正在测试的proc返回3个结果集。 My webAPI handles the multiple result sets and sends it to the UI fine. 我的webAPI处理多个结果集并将其发送到用户界面。

Question: In my SQL Server unit test, how do I handle the 3 result sets? 问题:在我的SQL Server单元测试中,如何处理3个结果集? If the proc returns one result set, it is easy to handle. 如果proc返回一个结果集,则很容易处理。 I use the following: 我使用以下内容:

Insert Into @ReturnData
(
 ID, 
 Data1, 
 Data2
)
Exec @Ret = StoreProcName

Then I can run a bunch of checks against the @ReturnData temp table. 然后我可以对@ReturnData临时表运行一堆检查。 But I don't understand how to handle/test a proc if it returns multiple result sets. 但是如果它返回多个结果集,我不明白如何处理/测试一个proc。 Is this even possible? 这甚至可能吗?

Thanks. 谢谢。

The method I'd suggest you use is tSQLt.ResultSetFilter() . 我建议你使用的方法是tSQLt.ResultSetFilter() This takes a parameter for number of the result set to return and calls your code under test (StoreProcName in your example), returning that result set, which you can then use Insert..Exec to capture. 这将获取要返回的结果集的数量的参数,并调用您的代码(在您的示例中为StoreProcName),返回该结果集,然后您可以使用Insert..Exec进行捕获。

The down side of this procedure is that it only captures that one result set per run - so you need to call it multiple times to return all of the result sets. 此过程的缺点是它每次运行只捕获一个结果集 - 因此您需要多次调用它以返回所有结果集。 I usually only look at one result set per test, allowing me to concentrate on answering one question in that test, but if your result sets interrelate and you need both to return for your test to be evaluated, then you will need to call tSQLt.ResultSetFilter and hence the code under test more than once in your test (the manual has more info on this situation) 我通常只查看每个测试的一个结果集,允许我专注于在该测试中回答一个问题,但是如果你的结果设置相互关联并且你需要返回以便评估你的测试,那么你将需要调用tSQLt。 ResultSetFilter ,因此测试中的代码不止一次( 手册中有关于这种情况的更多信息)

As an aside, I have previously blogged about some unexpected behaviour I encountered when using insert..exec with SPs that return multiple identical result sets which may be of interest. 顺便说一下,我之前曾在博客中介绍了在使用带有SP的insert..exec时遇到的一些意外行为,这些SP返回了多个可能感兴趣的相同结果集

DaveGreen has the answer. 戴夫格林有答案。 But for completeness, I wanted to share this which expands on the basics: http://tsqlt.org/201/using-tsqlt-resultsetfilter/ 但为了完整起见,我想分享一下扩展基础知识的内容: http//tsqlt.org/201/using-tsqlt-resultsetfilter/

If you call a stored procedure and need to pass in parameters, do the following: 如果调用存储过程并需要传入参数,请执行以下操作:

Create a @Variable that holds the 'exec …' string with the parameter values embedded. 创建一个@Variable ,其中包含嵌入参数值的'exec ...'字符串。 Then you can do something like this: 然后你可以做这样的事情:

Declare @Variable Varchar(max)
Set @Variable = ‘exec STOREDPROCNAME ‘’param1’’, ‘’param2’’’;
EXEC tSQLt.ResultSetFilter 2, @Variable

The number 2 specifies the second result set that is returned. 数字2指定返回的第二个结果集。

Nice and snappy ... ;-) 不错,活泼...... ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM