Problem: how to unit test stored procedure and function?
I have seen different posts about it and still don't know what is the right way.
What I tried: I created a project in Visual Studio to upload the database schema (containing my stored procedure and related function). From there I create another project with a unit test. I unfortunately however find it trivial and a bit useless.
My stored procedure is not big deal but I would still like to know if there is a way to unit test it properly.
Stored procedure:
CREATE PROCEDURE update_player
@Username NVARCHAR (250),
@Luck INT,
@DiceNumber INT
AS
UPDATE [dbo].[Player]
SET [Coins] = dbo.CalculatePlayerCoins(@Luck, @DiceNumber)
WHERE [Username] = @Username
GO
My function:
CREATE FUNCTION CalculatePlayerCoins
(@Luck INT, @DiceNumber INT)
RETURNS INT
AS
BEGIN
DECLARE @ret INT;
SELECT
@ret = @Luck * @DiceNumber * (SELECT Value FROM getRandomValue)
IF (@ret IS NULL)
SET @ret = 0;
RETURN @ret;
END;
getRandomValue
is a view that returns a random value.
I would like to use xUnit to test this if possible. Otherwise if you could point me to the right direction would be awesome. Thanks in advance!
tSQLt
if you want to write your unit tests in T-SQL.