SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


tSQLt - testing code in a CATCH block


tSQLt - testing code in a CATCH block

Author
Message
Ash.Shah
Ash.Shah
Say Hey Kid
Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)Say Hey Kid (674 reputation)

Group: General Forum Members
Points: 674 Visits: 390
Hello,

Yesterday I started writing my first tSQLt tests. Mostly with success, except for the following issue.

I have several stored procedures which follow a template where I create a record in a log table, insert some data in another table. If this second operation is successful, I execute a stored procedure to update the log record with a Status of Success. If the operation errors, processing transfers to a CATCH block, where I execute a stored procedure to update the log record with a Status of Failure.

So the procedure looks like this:

BEGIN TRY

EXEC InsertLogRecord /* insert the log record */

INSERT INTO ... /* doing the main purpose of the procedure */

EXEC UpdateLogRecord @LogID, @Status /* update log record with Success */

END TRY

BEGIN CATCH

EXEC UpdateLogRecord @LogID, @Status /* update log record with Failure */

RAISERROR /* throw an error (can't use THROW as in SQL2008R2) */

END CATCH

One of the tests I would like to write is check that the UpdateLogRecord procedure is called with expected parameters when an error occurs i.e. when transferred to the CATCH block.

I understand and have successfully used SpyProcedure. Where I am struggling is how best to cause my procedure to error in my test. The parameters I pass to the stored procedure don't lend themselves to easily raising an error (either varchars or XML).

At the moment I am relying on stubbing my InsertLogRecord to raise an error (using the optional @Command parameter of SpyProcedure). But that is a bit of a hack. I also need that procedure to return a LogID which I then pass to the UpdateLogRecord procedure.

I would appreciate any thoughts the community has and am happy to provide any further details required.

Many thanks,

Ash
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search