TILT- Tests- part 12

First, it is a decision between NUnit and XUnit. I took this time NUnit. Also, I take LightBDD to show data.

Let’s say I want to verify the rule that the user cannot make more than 1 TILT per day.

In order to do 1 TILT per day

  1. User must be authenticated and have have an URL registered into the database

  2. Code must verify that is no TILT for today ( i.e UTC Date)
  3. Code must insert the TILT into the database

I can work with a real database, but let’s see how we can Mock it – and run without database

This is the setup

namespace NetTilt.Tests;

[FeatureDescription(@"Test add a new TILT")]
[Label("FakesMocks")]
public partial class TestAdd
{
    ServiceProvider? serviceProvider;
    
    [SetUp]
    public void Setup()
    {

    }
    [Scenario]
    public async Task NewTilt() //scenario name
    {
       await Runner
            .AddSteps(_ => Given_No_TILT_For_URL())
            .AddAsyncSteps(_ => Then_Can_Add_A_New_TILT())
            .RunAsync();
    }
    [Scenario]
    public async Task Existing_TILT_Start_Of_The_Day() 
    {
        var minutes = DateTime.UtcNow.Subtract(DateTime.UtcNow.Date).TotalMinutes;
        minutes--;
        await Runner
              .AddSteps(_ => Given_Today_Is(DateTime.UtcNow))
             .AddSteps(_ => Given_Exists_One_TILT_ForDate(DateTime.UtcNow.AddMinutes(-minutes)))
             .AddAsyncSteps(_ => Then_Can_NOT_Add_A_New_TILT())
             .RunAsync();
    }
    [Scenario]
    [TestCase(2)]
    [TestCase(20)]
    [TestCase(1)]
    public async Task ExistingTILT_DaysAgo(int days) //scenario name
    {
        await Runner
              .AddSteps(_ => Given_Today_Is(DateTime.UtcNow))
             .AddSteps(_ => Given_Exists_One_TILT_ForDate(DateTime.UtcNow.AddDays(-days)))
             .AddAsyncSteps(_ => Then_Can_Add_A_New_TILT())
             .RunAsync();
    }
    [Scenario]
    [TestCase(1,false)]
    [TestCase(2, false)]
    [TestCase(20, false)]
    [TestCase(100, false)]
    [TestCase(200, false)]
    [TestCase(4*60, false)]
    [TestCase(5 * 60, false)]
    [TestCase(6 * 60, false)]
    [TestCase(7 * 60, false)]
    [TestCase(8 * 60, false)]
    [TestCase(9 * 60, false)]
    [TestCase(10 * 60, false)]
    [TestCase(11* 60, false)]
    [TestCase(12 * 60, false)]
    [TestCase(23 * 60, false)]
    [TestCase(23 * 60+58, false)]
    [TestCase(24 * 60 + 1, true)]
    public async Task ExistingTILT_MinutesAgo(int minutes, bool canAdd) //scenario name
    {
        var run =  Runner
              .AddSteps(_ => Given_Today_Is(DateTime.UtcNow))
              .AddSteps(_ => Given_Exists_One_TILT_ForDate(DateTime.UtcNow.Date.AddDays(1).AddMinutes(-minutes)));

        if (canAdd)
            run = run
             .AddAsyncSteps(_ => Then_Can_Add_A_New_TILT());
        else
            run = run
             .AddAsyncSteps(_ => Then_Can_NOT_Add_A_New_TILT());

        await run.RunAsync();
    }
    [Test]
    public async Task TestAddOneTilt()
    {
        var myTilt= serviceProvider.GetRequiredService<IMyTilts>();
        var data= await myTilt.AddTILT(new TILT_Note_Table(), null);
        Assert.IsNotNull(data);
    }
}

And this is the output of the tests in markdown

Results of tests (Passed:65)

TestAdd

Existing TILT Start Of The
Day

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
00:01:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT DaysAgo [days:
“2”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “04/29/2022
06:52:44”]
Passed
3 THEN Can Add A New TILT Passed

ExistingTILT DaysAgo [days:
“20”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “04/11/2022
06:52:44”]
Passed
3 THEN Can Add A New TILT Passed

ExistingTILT DaysAgo [days:
“1”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “04/30/2022
06:52:44”]
Passed
3 THEN Can Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“1”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
23:59:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“2”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
23:58:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“20”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
23:40:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“100”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
22:20:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“200”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
20:40:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“240”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
20:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“300”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
19:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“360”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
18:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“420”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
17:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“480”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
16:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“540”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
15:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“600”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
14:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“660”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
13:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“720”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
12:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“1380”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
01:00:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“1438”] [canAdd: “False”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “05/01/2022
00:02:00”]
Passed
3 THEN Can NOT Add A New TILT Passed

ExistingTILT MinutesAgo [minutes:
“1441”] [canAdd: “True”]

Number Name Status Comments
1 GIVEN Today Is [date: “05/01/2022
06:52:44”]
Passed
2 AND Exists One TILT ForDate [date: “04/30/2022
23:59:00”]
Passed
3 THEN Can Add A New TILT Passed

NewTilt


Number Name Status Comments
1 GIVEN No TILT For URL Passed
2 THEN Can Add A New TILT Passed

Tools used

Nunit

LightBdd

Microsoft.Extensions.DependencyInjection

Microsoft.NET.Test.Sdk Moq

NUnit

Visual Studio