Search code examples
tsqlt

tSQLt - Test that a column is output by a stored procedure


I'm very new to tSQLt and am having some difficulty with what should really be a very simple test.

I have added a column to the SELECT statement executed within a stored procedure.

How do I test in a tSQLt test that the column is included in the resultset from that stored procedure?


Solution

  • Generally, when adding a column to the output of a stored procedure, you will want to test that the column both exists and is populated with the correct data. Since we're going to make sure that the column is populated with the same data, we can design a test that does exactly that:

    CREATE PROCEDURE MyTests.[test stored procedure values MyNewColumn correctly]
    AS
    BEGIN
      -- Create Actual and Expected table to hold the actual results of MyProcedure 
      -- and the results that I expect
      CREATE TABLE MyTests.Actual (FirstColumn INT, MyNewColumn INT);
      CREATE TABLE MyTests.Expected (FirstColumn INT, MyNewColumn INT);
    
      -- Capture the results of MyProcedure into the Actual table
      INSERT INTO MyTests.Actual
      EXEC MySchema.MyProcedure;
    
      -- Create the expected output
      INSERT INTO MyTests.Expected (FirstColumn, MyNewColumn)
      VALUES (7, 12);
      INSERT INTO MyTests.Expected (FirstColumn, MyNewColumn)
      VALUES (25, 99);
    
    
      -- Check that Expected and Actual tables contain the same results
      EXEC tSQLt.AssertEqualsTable 'MyTests.Expected', 'MyTests.Actual';
    END;
    

    Generally, the stored procedure you are testing relies on other tables or other stored procedures. Therefore, you should become familiar with FakeTable and SpyProcedure as well: http://tsqlt.org/user-guide/isolating-dependencies/