Skip to content

Helpful Tips

Jason Mace edited this page Oct 18, 2018 · 2 revisions

Dealing with Timestamps/Dates

If the date is inconsequential, the best way to deal with it is to add it to the list of columns to ignore. There are times, though, when the date matters and you need to have a way of generating reliable, predictable values.

One method is to create a wrapper function, as shown below.

ALTER FUNCTION dbo.getDateWrapper()
RETURNS DATETIME
BEGIN
    RETURN GETDATE()
END
GO

This can be used in your procedure or function like so:

-- Call it once and store the result so T-SQL doesn't call it for each row
DECLARE @date DATETIME = dbo.getDateWrapper()
-- ...
SELECT @date

This can then be mocked in JUTS:

-- Mock the date function
EXEC juts.addCommandToScenario
    @scenarioId = @scenarioId
,   @command = 'ALTER FUNCTION dbo.getDateWrapper() RETURNS DATE BEGIN RETURN ''20200101'' END'

As expected, the wrapper function will be rolled back at the end of the test.

Clone this wiki locally