我正在编写一些 tSQLt 测试并通过 Visual Studio 的测试资源管理器运行它们tSQLt 测试适配器 https://the.agilesql.club/blogs/Ed-Elliott/tSQLt-Visual-Studio-Test-Adapter扩大。我正在做TDD https://en.wikipedia.org/wiki/Test-driven_development,所以我在编写它测试的存储过程之前编写测试。
问题是,当我运行测试时,它应该失败,因为存储过程尚不存在。当我在 Sql Server Management Studio 中使用 tSQLt 运行测试时,它失败了,就像它应该的那样:
The module 'test_ValidCustomerName_CustomerIdIs1' depends on the missing object 'dbo.AddCustomer'. The module will still be created; however, it cannot run successfully until the object exists.
(0 row(s) affected)
[AddCustomer].[test_ValidCustomerName_CustomerIdIs1] failed: (Error) Could not find stored procedure 'dbo.AddCustomer'.[16,62]{test_ValidCustomerName_CustomerIdIs1,7}
+----------------------+
|Test Execution Summary|
+----------------------+
|No|Test Case Name |Dur(ms)|Result|
+--+----------------------------------------------------+-------+------+
|1 |[AddCustomer].[test_ValidCustomerName_CustomerIdIs1]| 0|Error |
-----------------------------------------------------------------------------
Msg 50000, Level 16, State 10, Line 1
Test Case Summary: 1 test case(s) executed, 0 succeeded, 0 failed, 1 errored.
-----------------------------------------------------------------------------
但是当我在测试资源管理器中运行它时,它说测试通过了:
这是测试代码:
EXEC tSQLt.NewTestClass 'AddCustomer';
GO
CREATE PROCEDURE AddCustomer.test_ValidCustomerName_CustomerIdIs1
AS
BEGIN
DECLARE @customerName NVARCHAR(40) = 'John Doe'
EXEC dbo.AddCustomer @customerName
DECLARE @expected INT = 1
, @actual INT = (SELECT CustomerID
FROM dbo.Customer
WHERE Name = @customerName)
EXEC tSQLt.AssertEquals @expected, @actual
END
GO
这是dbo.Customer
table:
CREATE TABLE dbo.Customer
(
CustomerID INT NOT NULL PRIMARY KEY IDENTITY(1, 1)
, Name NVARCHAR(50) NOT NULL
)
EDIT- 我已将测试修改为仅调用tSQLt.Fail
:
EXEC tSQLt.NewTestClass 'AddCustomer';
GO
CREATE PROCEDURE AddCustomer.test_ValidCustomerName_CustomerIdIs1
AS
BEGIN
EXEC tSQLt.Fail 'Test should fail'
END
GO
在 Sql Server Management Studio 中测试仍然失败,但它passes在测试资源管理器中。