将tSQLt测试添加到现有生产产品中,因此我们无法更改表,约束等。当前,所有约束如“ PK_ViolationCategory”都标记为“ PK_dbo.ViolationCategory”
当我跑步时:
EXEC tSQLt.NewTestClass 'AdHocReportFiltersTestConstraint';
GO
CREATE PROCEDURE [AdHocReportFiltersTestConstraint].[Setup]
AS
BEGIN
EXEC tSQLt.FakeTable 'dbo.AdHocReports'
END
GO
CREATE PROCEDURE [AdHocReportFiltersTestConstraint].[test_AdHocReportFilters_Constraint]
AS
BEGIN
DECLARE @Id uniqueidentifier = NEWID()
DECLARE @Name NVARCHAR(Max) = 'Test_Name'
DECLARE @Value NVARCHAR(Max) = 'Test_Value'
DECLARE @AdHocReport_ID uniqueidentifier = NEWID()
INSERT INTO dbo.AdHocReportFilters ([Id], [Name], [Value], [AdHocReport_Id])
VALUES (@Id, @Name, @Value, @AdHocReport_ID)
exec tSQLt.ApplyConstraint 'dbo.AdHocReports', 'PK_dbo.AdHocReportFilters';
END
EXEC tSQLt.RunTestClass 'AdHocReportFiltersTestConstraint';
GO
我收到错误消息
(1 row affected)
[AdHocReportFiltersTestConstraint].[test_AdHocReportFilters_Constraint] failed:
(Error) The INSERT statement conflicted with the FOREIGN KEY constraint
"FK_dbo.AdHocReportFilters_dbo.AdHocReports_AdHocReport_Id". The conflict occurred in
database "CR", table "dbo.tSQLt_tempobject_fbc9c8bf09e742929eccae914d5e440d",
column 'Id'.[16,0]{test_AdHocReportFilters_Constraint,11}
关于如何解决此问题的任何想法?
一旦我开始工作,我将添加第二条记录以违反PK约束并捕获错误。
答案 0 :(得分:1)
仅查看您的代码,我就会看到您正在执行以下操作:
您收到的错误似乎表明您违反了 AdHocReportFilters 表上的外键-这是预期的,因为该表未被伪造。
从测试名称中还不清楚您是要验证主键还是外键的行为。
看看这些步骤,我认为您可能会混淆两个表,但是没有更详细的代码(即CREATE TABLE)语句,我很难为您提供进一步的帮助。
答案 1 :(得分:0)
有两个表:
dbo.AdHocReports (Id,... (more columns))
dbo.AdHocReportFilters ([Id], [Name], [Value], [AdHocReport_Id] )
AdHocReportFilters.AdHocReport_Id是表dbo.AdHocReportFilters引用dbo.AdHocReports.Id的外键
要插入的对象在表AdHocReportFilters
中插入一行,您应确保 AdHocReportFilters.AdHocReport_Id
是表id column
中的dbo.AdHocReports
>
但是在您的SP中,@AdHocReport_ID
是NEWID()
,这当然是唯一的值,因此不在表dbo.AdHocReports.Id
中
变通,
禁用外键约束
ALTER TABLE dbo.AdHocReportFilters NOCHECK CONSTRAINT FK_dbo.AdHocReportFilters_dbo.AdHocReports_AdHocReport_Id