Nunit beginnings

Hi

I have never written tests before but made a quick start this morning using Nunit on a basic tile class. Is the couple of tests I have written below heading in the right direction? The tests pass but I have no idea if I am doing the correct assertions or not.

`using System;
using ConsoleApp1;
using NUnit.Framework;

namespace Tests
{
[TestFixture]
public class TestTileDirt
{
    private TileDirt dirtTile;
    
    [SetUp]
    public void Setup()
    {
        dirtTile = new TileDirt(10, 10, TypeOfTile.Dirt);
    }

    [Test]
    public void TestSettingProperties()
    {
        dirtTile.PositionX = 15;
        dirtTile.PositionY = 15;
        dirtTile.TType = TypeOfTile.Dirt; 
            
        Assert.True(dirtTile.PositionX == 15);
        Assert.True(dirtTile.PositionY == 15);
        Assert.True(dirtTile.TType.GetType() == typeof(TypeOfTile));
    }

    [Test]
    public void TestUpdateTileType()
    {
        dirtTile.UpdateTileType(TypeOfTile.Sky);
        
        // Assert type changing to is of type TypeOfTile
        Assert.AreSame(typeof(TypeOfTile), dirtTile.TType.GetType());
    }

    [Test]
    public void TestUpdateTilePosition()
    {
        dirtTile.UpdateTilePosition(20, 20);
        
        // Assert the object values are now the new expected values
        Assert.True(dirtTile.PositionX == 20);
        Assert.True(dirtTile.PositionY == 20);
    }
}
}`

Tile and DirtTile Class code

namespace ConsoleApp1
{
    public abstract class Tile
    {
        protected TypeOfTile TileType { get; set; }
        protected int PosX { get; set; }
        protected int PosY { get; set; }

        public TypeOfTile TType
        {
            get => TileType;
            set => TileType = value;
        }

        public int PositionX
        {
            get => PosX;
            set => PosX = value;
        }

        public int PositionY
        {
            get => PosY;
            set => PosY = value;
        }
    }
}

namespace ConsoleApp1
{
    public class TileDirt : Tile, ITileDrawable
    {
        public TileDirt(int x, int y, TypeOfTile t)
        {
            TileType = t;
            PositionX = x;
            PositionY = y;
        }

        public void Draw()
        {
            //Console.Out()
        }

        public void UpdateTilePosition(int x, int y)
        {
            PositionX = x;
            PositionY = y;
        }

        public void UpdateTileType(TypeOfTile t)
        {
            TileType = t;
        }
    }
}

Yes those tests look correct.

As a personal preference, I like to only have one check in each unit test. For example, if the X position is wrong in TestSettingProperties, the test will assert out and won’t check Y or TType. I’d break it up into 3 different tests:

[Test]
public void TestSettingProperties_X()
{
     dirtTile.PositionX = 15;
     dirtTile.PositionY = 15;
     dirtTile.TType = TypeOfTile.Dirt; 
            
     Assert.True(dirtTile.PositionX == 15);
}

public void TestSettingProperties_Y()
{
     dirtTile.PositionX = 15;
     dirtTile.PositionY = 15;
     dirtTile.TType = TypeOfTile.Dirt; 
           
     Assert.True(dirtTile.PositionY == 15);
}

public void TestSettingProperties_TileType()
{
     dirtTile.PositionX = 15;
     dirtTile.PositionY = 15;
     dirtTile.TType = TypeOfTile.Dirt; 
            
     Assert.True(dirtTile.TType.GetType() == typeof(TypeOfTile));
}

Also I’m a big fan of the Shouldly package:

Cheers!