2017-04-11 43 views

回答

0

在運行測試之前,您可以嘗試EXEC [tSQLt].[XmlResultFormatter];。這是爲了在「構建服務器」場景中使用,但可能會被迫服務以向您展示更多SSMS中的輸出。

1

這取決於您實際測試的內容。我同意,在廣泛的結果集AssertResultsSetsHaveSameMetaData的輸出可能有點笨拙。對於存儲過程,你會寫你的測試是這樣的:

 
create procedure [ProcedureTests].[test SelectProcedure result set contract] 
as 
begin 
    create table #expected 
    (
     name varchar(500) not null 
    , column_ordinal int not null identity(1,1) 
    , system_type_name varchar(500) not null 
    , Nullability varchar(16) not null 
    ) 

    ; with expectedCte (name, system_type_name, Nullability) 
    as 
    (
        select 'ItemId'       , 'int'    , 'not null' 
     union all select 'ActorId'       , 'int'    , 'not null' 
     union all select 'LanId'       , 'nvarchar(200)' , 'not null' 
     union all select 'ConsumerId'      , 'int'    , 'not null' 
     union all select 'ConsumerMoniker'     , 'nvarchar(200)' , 'not null' 
     union all select 'ProfileTypeId'     , 'int'    , 'not null' 
     union all select 'ProfileTypeName'     , 'varchar(50)'  , 'not null' 
     union all select 'ProfileId'      , 'int'    , 'not null' 
    ) 
    insert #expected 
    (
     name 
    , system_type_name 
    , Nullability 
    ) 
    select name, system_type_name, Nullability from expectedCte; 

    --! Act 
    select 
      name 
     , column_ordinal 
     , system_type_name 
     , case is_nullable when 1 then 'null' else 'not null' end as [Nullability] 
    into 
     #actual 
    from 
     sys.dm_exec_describe_first_result_set_for_object(object_id('mySchema.SelectProcedure'), 0); 

    --! Assert 
    exec tSQLt.AssertEqualsTable #expected, #actual; 
end; 
go 

雖然對意見,你可以使用這個(略有不同)的方法:

 
alter procedure [ViewTests].[test ViewName resultset contract] 
as 
begin 
    create table [ViewTests].[expected] 
    (
     TransactionId int not null 
    , SourceId int not null 
    , SourceKey nvarchar(50) not null 
    , TransactionTypeId int not null 
    , TransactionStatusId int not null 
    , LastModified datetime not null 
    ); 
    --! You comparison may be as simple as this (but see alternative approach below) 
    exec tSQLt.AssertEqualsTableSchema '[ViewTests].[expected]', 'mySchema.ViewName'; 


    --! 
    --! Seems that is_nullable column on dm_exec_describe_first_result_set (used by tSQLt.AssertEqualsTableSchema) 
    --! can be a bit flakey where views are concerned so you may need to ignore nullability when testing 
    --! this view (so comment out that column in both SELECTs) 
    --! 
    select 
      c.name as [ColumnName] 
     , c.column_id as [ColumnPosition] 
     , case 
      when st.name in ('char', 'varchar', 'varbinary') 
       then st.name + '(' + case when c.max_length = -1 then 'max' else coalesce(cast(c.max_length as varchar(8)), '???') end + ')' 
      when st.name in ('nchar', 'nvarchar') 
       then st.name + '(' + case when c.max_length = -1 then 'max' else coalesce(cast(c.max_length/2 as varchar(8)), '???') end + ')' 
      when st.name in ('decimal', 'numeric') 
       then st.name + '(' + coalesce(cast(c.precision as varchar(8)), '???') + ',' + coalesce(cast(c.scale as varchar(8)), '???') + ')' 
      when st.name in ('time', 'datetime2', 'datetimeoffset') 
       then st.name + '(' + coalesce(cast(c.precision as varchar(8)), '???') + ')' 
      else st.name 
      end as [DataType] 
     , c.[precision] as [NumericScale] 
     , c.scale as [NumericPrecision] 
     , c.collation_name as [CollationName] 
     , cast(case c.is_nullable when 1 then 'null' else 'not null' end as varchar(16)) as [Nullability] 
    into 
     #expected 
    from 
     sys.columns as c 
    inner join sys.types as st 
     on st.system_type_id = c.system_type_id 
     and st.user_type_id = c.user_type_id 
    where 
     c.[object_id] = object_id('[ViewTests].[expected]') 

    select 
      name as [ColumnName] 
     , column_ordinal as [ColumnPosition] 
     , system_type_name as [DataType] 
     , [precision ]as [NumericScale] 
     , scale as [NumericPrecision] 
     , collation_name as [CollationName] 
     , cast(case is_nullable when 1 then 'null' else 'not null' end as varchar(16)) as [Nullability] 
    into 
     #actual 
    from 
     sys.dm_exec_describe_first_result_set('select * from mySchema.ViewName, null, null) 

    exec tSQLt.AssertEqualsTable '#expected', '#actual' ; 
end 
go 

在即使發生任何故障,之所以會很多因爲輸出更像AssertEqualsTable。