2

ExpectException を使用するたびに、次のエラーが表示されます。

tSQLt は、MSSQL が RAISERROR のために ROLLBACK を既に実行したという事実の後で、ROLLBACK を試みているようです。次の SELECT および SET ステートメントを Private_RunTest で次の IF ステートメントでラップすると、問題が解決したように見えました。

IF ISNULL(@ExpectException,0) <> 1 
  BEGIN
    SELECT @Msg = COALESCE(@Msg, '') + ' (There was also a ROLLBACK ERROR --> ' + 
                  COALESCE(ERROR_MESSAGE(), '') + '{' + 
                  COALESCE(ERROR_PROCEDURE(), '') + ',' + 
                  COALESCE(CAST(ERROR_LINE() AS NVARCHAR), '') + '})';
    SET @Result = 'Error';
  END

これは本当にバグですか、それとも適切な修正ですか?

4

2 に答える 2

5

tsqlt テストで TRY/CATCH ブロックを使用していない可能性があります。

エラー メッセージ " There was also a ROLLBACK ERROR --> The current transaction cannot be commit and cannot be roll back to a savepoint. Roll back the whole transaction. " は、次のように再現できます。

1) エラーをスローしてロールバックするトリガーを次のように作成します。

CREATE TRIGGER [dbo].[MyTable_IUDTR] ON [dbo].[MyTable]
    AFTER INSERT, UPDATE, DELETE
AS 
BEGIN

  BEGIN TRY
      RAISERROR('MyError', 16, 1)
  END TRY

  BEGIN CATCH
      THROW;
  END CATCH

END
GO

2) tsqlt テストを作成して、返されたエラー メッセージを確認します。

CREATE PROC [ut_MyTable_IUDTR].[test that the error is returned]
AS
BEGIN

  DECLARE @ErrorMsg VARCHAR(50)

  EXEC tsqlt.FakeTable @TableName = 'MyTable'
  EXEC tsqlt.ApplyTrigger 'MyTable', 'MyTable_IUDTR'

  INSERT INTO dbo.MyTable
          ( FirstName, LastName )
  VALUES  ( N'John',N'Smith')

  SET @ErrorMsg = ERROR_MESSAGE()

  EXEC tSQLt.AssertEqualsString @Expected = 'MyError', @Actual = @ErrorMsg

END
GO

3) テストを実行します。

EXEC [tSQLt].Run 'ut_MyTable_IUDTR.test that the error is returned'

4) 次のエラーが表示されます。

    There was also a ROLLBACK ERROR --> The current transaction cannot be committed and cannot be rolled back to a savepoint. Roll back the entire transaction.

修正:

次のように、TRY/CATCH ブロックを含めるように tsqlt テストを変更します。

ALTER PROC [ut_MyTable_IUDTR].[test that the error is returned]
AS
BEGIN

  DECLARE @ErrorMsg VARCHAR(50)

  EXEC tsqlt.FakeTable @TableName = 'MyTable'
  EXEC tsqlt.ApplyTrigger 'MyTable', 'MyTable_IUDTR'

  BEGIN TRY
      INSERT INTO dbo.MyTable
              ( FirstName, LastName )
      VALUES  ( N'John',N'Smith')
  END TRY

  BEGIN CATCH
      SET @ErrorMsg = ERROR_MESSAGE()
  END CATCH

  EXEC tSQLt.AssertEqualsString @Expected = 'MyError', @Actual = @ErrorMsg

END
GO
于 2016-06-03T01:11:43.437 に答える
0

SET XACT_ABORT ONtsqlt テストで使用している可能性があります

CREATE PROC [testClass].[test foo]
なので
始める
  XACT_ABORT をオンに設定
  EXEC tsqlt.AssertEquals @Expected = 1、@Actual = 0
終わり

これをしないでください。

このようなメッセージが届きます

[testClass].[test foo] failed: (Error) Expected: <1> but was: <0> (There was also a ROLLBACK ERROR --> The current transaction cannot be committed and cannot be rolled back to a savepoint. Roll back the entire transaction.{Private_RunTest,160})

于 2016-11-22T00:19:50.377 に答える