Question

I'm experimenting with unit testing of a SQL Server Database Project in Visual Studio 2013. The output of the stored procedure I want to test is written to a log file by calling another stored procedure, sp_log.

It seems to me the easiest way to check whether the stored procedure I want to test is working would be to mock sp_log and use a test condition to check the output of the mocked stored procedure.

I could create a mock sp_log in the pre-test script, no problem. However, in the post-test script I would like to re-execute the sp_Log CREATE PROC script to restore it to its original definition. I don't want to cut and paste the sp_Log CREATE PROC from the SQL Server Database Project under test as I'm sure the two versions of the CREATE PROC (in the SQL Server Database Project and in the Unit Test Project) would get out of sync.

Is there any way I could call the sp_Log CREATE PROC script in the SQL Server Database Project from the post-test script in the Unit Test project?

Alternatively, is there any way to record the definition of sp_log in the pre-test script then re-run that definition in the post-test script? E.g. is it possible to create a local variable in the pre-test script and read the stored procedure's definition into it, then read the definition back out of the local variable in the post-test script?

Was it helpful?

Solution 2

Try renaming sp_log in the pre-test script, then rename it back in the post-test script.

Pre-test:

EXEC sp_rename 'sp_log', 'sp_log_BACKUP'
CREATE PROCEDURE sp_log AS .... /* your mock sp_log */

Post-test

EXEC sp_rename 'sp_log_BACKUP', 'sp_log'

I'm not sure of a way to reference the stored procedure definitions in variables or something similar. Hopefully renaming the stored procedure as I have suggested is sufficient.

OTHER TIPS

Just to confirm the renaming technique works, in fact I got it demo-ed on a post:

https://tangodude.wordpress.com/2014/02/08/ssdt-database-projects-mocking-database-objects-for-isolated-unit-testing-kind-of/

Sorry for the redundant answer, but don't have enough rep to comment yet. :)

To ensure your database remains in the same state after test execution, I execute it within a transaction. By doing so I do not need to worry about cleaning up the database after test execution. You can find more details here: http://msdn.microsoft.com/en-us/library/aa833153(v=vs.100).aspx

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top