Question

I'm creating a SQL Server unit test using tSQLt.

The proc that I'm testing returns 3 result sets. My webAPI handles the multiple result sets and sends it to the UI fine.

Question: In my SQL Server unit test, how do I handle the 3 result sets? If the proc returns one result set, it is easy to handle. 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. But I don't understand how to handle/test a proc if it returns multiple result sets. Is this even possible?

Thanks.

Was it helpful?

Solution

The method I'd suggest you use is 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.

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)

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.

OTHER TIPS

DaveGreen has the answer. But for completeness, I wanted to share this which expands on the basics: 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. 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.

Nice and snappy ... ;-)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top