Possible with multiple database connections

余生颓废 提交于 2019-12-20 06:44:55

问题


New to the tSQLt world (great tool set) and encountered a minor issue with a stored procedure I am setting up a test for.

If I for some reason have a stored procedure which connects to mutiple databases or even multiple SQL servers (Linked Servers).

Is it possible to do unit tests with tSQLt in such a scenario?


回答1:


I commented already, but I would like to add some more. So as I said already, that you can do anything that fits into the single transaction.

But for your case I would suggest to create synonyms for every cross database/instance object and then use synonyms everywhere.

I've created following function to mock view/tables synonyms. It has some limitations but at least it can handle simple use cases.

CREATE PROCEDURE [tSQLt].[FakeSynonymTable] @SynonymTable VARCHAR(MAX)
AS
     BEGIN

         DECLARE @NewName VARCHAR(MAX)= @SynonymTable+REPLACE(CAST(NEWID() AS VARCHAR(100)), '-', '');
         DECLARE @RenameCmd VARCHAR(MAX)= 'EXEC sp_rename '''+@SynonymTable+''', '''+@NewName+''';';

        EXEC tSQLt.SuppressOutput
              @RenameCmd;

        DECLARE @sql VARCHAR(MAX)= 'SELECT * INTO '+@SynonymTable+' FROM '+@NewName+' WHERE 1=2;';

        EXEC (@sql);

        EXEC tSQLt.FakeTable
              @TableName = @SynonymTable;
     END; 



回答2:


Without you providing sample code I am not certain of your exact use case but this information may help.

The alternative approach for cross-database testing (assuming both databases are on the same instance) is to install tSQLt in both databases. Then you can mock the objects in the remote database in the same way that you would if they were local.

E.g. If you had a stored procedure in LocalDb that referenced a table in RemoteDb, you could do something like this:

Imagine you have a procedure that selects a row from a table called localTable in the local database and inserts that row in to a table called remoteTable in the remote database (on the same instance)

create procedure [myTests].[test mySproc inserts remoteTable from local table]
as
begin
    -- Mock the local table in the local database
    exec tSQLt.FakeTable 'dbo.localTable' ;
    -- Mock the remote table (not the three part object reference to remoteDb)
    exec RemoteDb.tSQLt.FakeTable 'dbo.remoteTable' ;

    --! Data setup ommitted

    --! exec dbo.mySproc @param = 'some value' ;

    --! Get the data from the remote table into a temp table so we can test it
    select * into #expected from RemoteDb.dbo.remoteTable;

    --! Assume we have already populated #actual with our expected results
    exec tSQLt.AssertEqualsTable '#expected', '#actual' ;
end

The above code demonstrates the basics but I blogged about this in more detail some years ago here.

Unfortunately this approach will not work across linked servers,



来源:https://stackoverflow.com/questions/47708116/possible-with-multiple-database-connections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!