Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Unit Testing in C# Writing Unit Tests First Test

working on unit testing - what should I be doing here? I made an attempt but feel I am missing the point

it says to call the OnMap method. I think I tried that did I do this in the right file?

Map.cs
namespace TreehouseDefense
{
    public class Map
    {
        public readonly int Width;
        public readonly int Height;

        public Map(int width, int height)
        {
            if(width < 1 || height < 1)
            {
                throw new System.ArgumentOutOfRangeException(
                    "Map must be at least 1x1");
            }

            Width = width;
            Height = height;
        }

        public bool OnMap(Point point)
        {
            return point.X >= 0 && point.X < Width && 
                   point.Y >= 0 && point.Y < Height;
        }
    }
}
MapTests.cs
using Xunit;

namespace TreehouseDefense.Tests
{
    public class MapTests
    {
        [Fact]
        public void OnMapTest()
        {
            OnMap();
            Assert.True(false, "This test needs an implementation");
            Assert.True(true, "This test is true");

        }
    }
}
Point.cs
using System;

namespace TreehouseDefense
{
    public class Point
    {
        public readonly int X;
        public readonly int Y;

        public Point(int x, int y)
        {
            X = x;
            Y = y;
        }

        public double DistanceTo(Point point)
        {
            return Math.Sqrt(Math.Pow(X - point.X, 2.0) + Math.Pow(Y - point.Y, 2.0));
        }
    }
}

1 Answer

andren
andren
28,558 Points

You need to test if passing a valid point to onMap actually results in true being returned.

In order to do that you have to do all of the things you would normally do if you wanted to use the onMap method. You have to create a Point instance and a Map instance. Then you need to pass the point to the map's onMap method.

You then need to use the Assert.True method. That method takes a boolean (true or false) as the first argument, if the boolean is true it will pass the test, if it is false then the test will fail.

thanks I will look through this and try again later!