这是我与tsqlt
在一起的第一天,所以您可以期待一些模糊的陈述。
我正在尝试测试具有Try Catch Block
的storedProcedure,但是测试中的实际语句是insert和update命令。
现在,我想测试是否发生ErrorRaised时我的catch块是否执行了预期的任务。
能否请您指导我如何在测试中从存储过程中引发错误,而我们内部没有任何可模拟/伪造的内容。
希望我的问题是可以理解的,并在需要时乐意澄清。
最佳答案
因此,如果我正确理解了您的问题,那么您正在尝试测试您的catch块是否有效?
执行此操作的方法将取决于catch块中发生的情况。想象一下这种情况:
create table mySimpleTable
(
Id int not null primary key
, StringVar varchar(8) null
, IntVar tinyint null
)
go
我们有一个存储过程,可以将数据插入此表中。
这是基于我在许多过程中使用的模板的。它首先验证输入,然后执行所需的工作。命名每个步骤对于了解在更复杂的多步骤过程中发生错误的位置特别有用。 catch块使用我的Log4TSql日志框架,您可以阅读有关on my blog的更多信息并从SourceForge下载。
我遵循的模式是捕获有关异常以及在catch块内发生错误时程序正在执行的操作的信息,但要确保在程序结束时仍引发错误。您还可以选择在catch块内调用raiserror(在SQL2012上也为
throw
)。无论哪种方式,我都认为,如果某个过程遇到异常,则应始终将其通知整个链(即永不隐藏)。create procedure mySimpleTableInsert
(
@Id int
, @StringVar varchar(16) = null
, @IntVar int = null
)
as
begin
--! Standard/ExceptionHandler variables
declare @_FunctionName nvarchar(255) = quotename(object_schema_name(@@procid))
+ '.' + quotename(object_name(@@procid));
declare @_Error int = 0;
declare @_ReturnValue int;
declare @_RowCount int = 0;
declare @_Step varchar(128);
declare @_Message nvarchar(1000);
declare @_ErrorContext nvarchar(512);
begin try
set @_Step = 'Validate Inputs'
if @Id is null raiserror('@Id is invalid: %i', 16, 1, @Id);
set @_Step = 'Add Row'
insert dbo.mySimpleTable (Id, StringVar, IntVar)
values (@Id, @StringVar, @IntVar)
end try
begin catch
set @_ErrorContext = 'Failed to add row to mySimpleTable at step: '
+ coalesce('[' + @_Step + ']', 'NULL')
exec log4.ExceptionHandler
@ErrorContext = @_ErrorContext
, @ErrorProcedure = @_FunctionName
, @ErrorNumber = @_Error out
, @ReturnMessage = @_Message out
;
end catch
--! Finally, throw any exception that will be detected by the caller
if @_Error > 0 raiserror(@_Message, 16, 99);
set nocount off;
--! Return the value of @@ERROR (which will be zero on success)
return (@_Error);
end
go
让我们开始创建一个新的架构(类)来保存我们的测试。
exec tSQLt.NewTestClass 'mySimpleTableInsertTests' ;
go
我们的第一个测试是最简单的,并且仅检查即使我们的catch块捕获了异常,该过程仍然返回错误。在此测试中,我们仅使用
exec tSQLt.ExpectException
来检查@Id提供为NULL时是否引发了错误(这使我们的输入验证检查失败)create procedure [mySimpleTableInsertTests].[test throws error from catch block]
as
begin
exec tSQLt.ExpectException @ExpectedErrorNumber = 50000;
--! Act
exec dbo.mySimpleTableInsert @Id = null
end;
go
我们的第二个测试稍微复杂一点,它使用
tsqlt.SpyProcedure
来“模拟”异常处理程序,否则它将记录异常。在幕后,当我们以这种方式模拟一个过程时,tSQLt将创建一个表,该表以被监视的过程命名,并用仅将输入参数值写入该表的过程替换被监视的过程。在测试结束时,所有这些都会回滚。这使我们可以检查ExceptionHandler是否已调用以及将哪些值传递给它。在此测试中,我们检查由于输入验证错误而导致mySimpleTableInsert调用了ExceptionHander。create procedure [mySimpleTableInsertTests].[test calls ExceptionHandler on error]
as
begin
--! Set the Error returned by ExceptionHandler to zero so the sproc under test doesn't throw the error
exec tsqlt.SpyProcedure 'log4.ExceptionHandler', 'set @ErrorNumber = 0;';
select
cast('Failed to add row to mySimpleTable at step: [Validate inputs]' as varchar(max)) as [ErrorContext]
, '[dbo].[mySimpleTableInsert]' as [ErrorProcedure]
into
#expected
--! Act
exec dbo.mySimpleTableInsert @Id = null
--! Assert
select
ErrorContext
, ErrorProcedure
into
#actual
from
log4.ExceptionHandler_SpyProcedureLog;
--! Assert
exec tSQLt.AssertEqualsTable '#expected', '#actual';
end;
go
最后,以下(有些人为设计)的示例使用相同的模式来检查@IntVar的值对于表而言是否太大并捕获到错误:
create procedure [mySimpleTableInsertTests].[test calls ExceptionHandler on invalid IntVar input]
as
begin
--! Set the Error returned by ExceptionHandler to zero so the sproc under test doesn't throw the error
exec tsqlt.SpyProcedure 'log4.ExceptionHandler', 'set @ErrorNumber = 0;';
select
cast('Failed to add row to mySimpleTable at step: [Add Row]' as varchar(max)) as [ErrorContext]
, '[dbo].[mySimpleTableInsert]' as [ErrorProcedure]
into
#expected
--! Act
exec dbo.mySimpleTableInsert @Id = 1, @IntVar = 500
--! Assert
select
ErrorContext
, ErrorProcedure
into
#actual
from
log4.ExceptionHandler_SpyProcedureLog;
--! Assert
exec tSQLt.AssertEqualsTable '#expected', '#actual';
end;
go
create procedure [mySimpleTableInsertTests].[test throws error on invalid IntVar input]
as
begin
exec tSQLt.ExpectException @ExpectedErrorNumber = 50000;
--! Act
exec dbo.mySimpleTableInsert @Id = 1, @IntVar = 500
end;
go
如果这不能回答您的问题,也许您可以发布一个示例来说明您要实现的目标。