tSQLt - Database Unit Testing for SQL Server

Database Unit Testing for SQL Server

  • Home
  • Docs
    • Documents
    • Quick Start
    • Articles
  • Training
  • Downloads
tSQLt » Documents » Isolating Dependencies » RemoveObject

RemoveObject

Syntax

tSQLt.RemoveObject [@ObjectName= ] 'object name'
                [, [@NewName = ] 'new object name' OUTPUT]
                [, [@IfExists = ] ( 0 | 1 )]

Arguments

[@ObjectName = ] ‘object name’
The name of the object to be renamed. The name should include both the schema and object name.
[@NewName = ] ‘new object name’ OUTPUT
The automatically generated name the object was given after being renamed. This is an output parameter only. If a value is provided here as input, it will not be used.
[@IfExists = ] ( 0 | 1 )
If @IfExists = 0 is passed, an error is raised if the object does not exist. @IfExists = 1 causes tSQLt.RemoveObject to be a NoOp if the object does not exist. The default is 0.

Return Code Values

Returns 0

Error Raised

If the specified object does not exist an error is thrown: %s does not exist.

Result Sets

None

Overview

Often times an object needs to be replaced with a mock. The first step in replacing an object with a mock is to remove the original object. tSQLt.RemoveObject removes the original object by renaming it. The new name for the object is automatically generated to avoid collisions with other objects.

For tables and stored procedures, please refer to FakeTable and SpyProcedure, respectively.

Examples

Example: Replacing a function with a stub
In this example, we are testing that GetRecentUsers view returns only users from the past 10 minutes. In this example, we have a util.GetCurrentDate function which returns the current date and is used by GetRecentUsers. For the purposes of our test, we want to return a constant date. RemoveObject is called so that we can create a stub function which returns a hard-coded value.

CREATE PROCEDURE GetRecentUsersTests.[test that GetRecentUsers returns users from the past 10 minutes]
AS
BEGIN
    EXEC tSQLt.FakeTable 'dbo.Users';
    INSERT INTO dbo.Users (username, startdate) VALUES ('bob', '2013-03-15 11:59:59');
    INSERT INTO dbo.Users (username, startdate) VALUES ('joe', '2013-03-15 12:00:00');
    INSERT INTO dbo.Users (username, startdate) VALUES ('sue', '2013-03-15 12:01:00');

    EXEC tSQLt.RemoveObject 'util.GetCurrentDate';
    EXEC ('CREATE FUNCTION util.GetCurrentDate() RETURNS datetime AS BEGIN RETURN ''2013-03-15 12:10:00''; END;');

    SELECT username, startdate
      INTO #Actual
      FROM dbo.GetRecentUsers;

    SELECT TOP(0) *
      INTO #Expected
      FROM #Actual;

    INSERT INTO #Expected (username, startdate) VALUES ('joe', '2013-03-15 12:00:00');
    INSERT INTO #Expected (username, startdate) VALUES ('sue', '2013-03-15 12:01:00');

    EXEC tSQLt.AssertEqualsTable '#Expected', '#Actual';
END;
GO

See Also

  • ApplyConstraint
  • ApplyTrigger
  • FakeFunction
  • FakeTable
  • RemoveObject
  • RemoveObjectIfExists
  • SpyProcedure

Since

V1.0.4822.19862
@IfExist since V1.0.5325.27056

Share and Enjoy:
  • FacebookFacebook
  • TwitterTwitter
  • LinkedInLinkedIn
  • RedditReddit
  • StumbleUponStumbleUpon
  • TechnoratiTechnorati
  • PrintPrint

Navigation

  • Full user guide
  • Why you should use the tSQLt framework
  • How to get more out of tSQLt
  • Join the conversation
  • Downloads
  • Articles
  • Release Notes
  • New Logo
  • Training
  • Sponsors & Contributors
  • Why you should unit test SQL Server Code
  • Documents
    • Quick Start
    • tSQLt Tutorial
    • tSQLt Keyboard Shortcuts
    • Test Creation and Execution
      • NewTestClass
      • DropClass
      • RunAll
      • Run
      • RenameClass
    • Assertions
      • AssertNotEquals
      • AssertEmptyTable
      • AssertEquals
      • AssertEqualsString
      • AssertEqualsTable
      • AssertObjectExists
      • AssertResultSetsHaveSameMetaData
      • Fail
      • AssertObjectDoesNotExist
      • AssertEqualsTableSchema
      • AssertLike
    • Expectations
      • ExpectException
      • ExpectNoException
    • Isolating Dependencies
      • ApplyConstraint
      • FakeTable
      • SpyProcedure
      • FakeFunction
      • RemoveObjectIfExists
      • ApplyTrigger
      • RemoveObject

Links

  • tSQLt on GitHub
  • tSQLt Mailing List
  • twitter hashtag (#tsqlt)
  • tSQLt tag on stackoverflow.com
  • SQL Server Community on Slack (#tsqlt)
  • #tSQLtLive on YouTube

Sponsors

sqlity.net
redgate.com

A sqlity.net llc Web Property. | ©2010 - 2020, All Rights Reserved. | Privacy Policy | Terms of Use