tSQLt.ApplyTrigger [@TableName = ] 'table name' , [@TriggerName = ] 'trigger name'
[@TableName = ] ‘table name’
The name of the table where the constraint should be applied. Should contain both the schema name and the table name.
[@TriggerName = ] ‘trigger name’
The name of the trigger to be applied. Should not include the schema name or table name.
Return Code Values
If the specified table or trigger does not exist an error is thrown.
We want to be able to test triggers individually. We can use FakeTable to remove all the constraints and triggers from a table, and ApplyTrigger to add back in the one which we want to test.
ApplyTrigger in combination with FakeTable allows triggers to be tested in isolation of constraints and other triggers on a table.
Example: Using ApplyTrigger to test a trigger
In this example, the test isolates the AuditInserts trigger from other constraints and triggers on the Registry.Student table. This allows us to test that the trigger inserts a record into the Logs.Audit table when a new student is inserted into the Student table.
EXEC tSQLt.NewTestClass 'AuditTests'; GO CREATE PROCEDURE AuditTests.[test inserting record into Student table creates Audit record] AS BEGIN EXEC tSQLt.FakeTable 'Registry.Student'; EXEC tSQLt.FakeTable @TableName = 'Logs.Audit'; EXEC tSQLt.ApplyTrigger 'Registry.Student', 'AuditInserts'; INSERT INTO Registry.Student (StudentId) VALUES (1); SELECT LogMessage INTO #Actual FROM Logs.Audit; SELECT TOP(0) * INTO #Expected FROM #Actual; INSERT INTO #Expected VALUES('Student record created, id = 1'); EXEC tSQLt.AssertEqualsTable '#Expected','#Actual'; END; GO
I've faked the source table with the trigger, the destination table, and applied the (update) trigger, yet when I create then update a row in the source table, the trigger doesn't fire and I never get a row in the destination table. Not surprisingly, I get a 'unexpected/missing resultset rows!' failure. Any pointers on how to proceed? Thanks.
In the syntax section the syntax for the ApplyTrigger command is incorrect. It should read tSQLt.ApplyTrigger, not tSQLt.ApplyConstraint
@Lee Anne Pedersen , I would need to see your test to provide an answer. You could post it on stackoverflow with the tSQLt tag. Answering there is a little easier than here in the comments.