I have a stored procedure that uses the GETUTCDATE() function several times. It's a very specific line of business sproc, so it probably wouldn't make a lot of sense to show it here. Having said that, it might be helpful to know the sproc will only ever be called for the current year. Here's a contrived example that doesn't show the complexity of what I'm doing, but should help illustrate what I'm talking about:
CREATE PROCEDURE dbo.GenerateRequestListForCurrentYear AS BEGIN
SELECT RequestId, StartDate, EndDate FROM Requests
WHERE YEAR(EndDate) = YEAR(GETUTCDATE());
END;
My test looks like this:
CREATE PROCEDURE testClass.[test Requests are generated for the current year] AS BEGIN
-- arrange
EXEC tSQLt.FakeTable 'dbo.Requests';
INSERT INTO dbo.Requests (RequestId, StartDate, EndDate) VALUES
(1, '2/1/14', '2/10/14'), (2, '2/1/13', '2/10/13');
SELECT TOP (0) * INTO #Expected FROM dbo.Requests;
SELECT TOP (0) * INTO #Actual FROM dbo.Requests;
INSERT INTO #Expected VALUES
(1, '2/1/14', '2/10/14');
-- act
INSERT INTO #Actual
EXEC dbo.GenerateRequestListForCurrentYear;
-- assert
EXEC tSQLt.AssertEqualsTable #Expected, #Actual;
END;
I see a couple options:
- Pass in the date/time as a parameter (and set the value to GETUTCDATE() if NULL)
- Replace the call to GETUTCDATE() with my own function to return the date/time that I can fake using tSQLt
- ?
Both of these seem like options that are only necessary for the test which seems a little smelly; there wouldn't ever be a need to parameterize this sproc with regard to how it's being called from the primary application.
As it relates to dependencies with built-in functions in SQL that have their own dependencies, is there a way to fake those calls in a tSQLt test? Is there a better way to fake the call to GETUTCDATE() to return a date I specify in my tests using tSQLt or are these my only options?