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

First Unit test HELP!

Fill out MapTests.OnMapTest to test that Map.OnMap returns true when passed a point that is on the map.

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()
        {
            Width = 1;
            Height = 1;
            OnMap(Point point)
            Assert.True(false, "This test needs an implementation");
        }
    }
}
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));
        }
    }
}

3 Answers

Mohammad Laif
PLUS
Mohammad Laif
Courses Plus Student 22,297 Points

You need to create a point and a map. Then call the onMap() from your map object and pass your point there. FYI, your point need to be inside your map range, so it could return true.

using Xunit;

namespace TreehouseDefense.Tests
{
    public class MapTests
    {
        [Fact]
        public void OnMapTest()
        {
            var myPoint = new Point(1,1);
            var myMap = new Map(4,4);
            Assert.True(myMap.OnMap(myPoint), "This test needs an implementation");
        }
    }
}
Alex Hedley
Alex Hedley
16,381 Points

For your Test OnMapTest create a bool and assign it the value returned by OnMap

You are calling the method OnMap() which is passed a point so you need to create a point object, assign it an x and y and pass that to OnMap

Point p = new Point(1, 1);
bool oM = OnMap(p);

i need another detailed explanation for this one. i don't see where the four comes into play

My understanding is that the parameters of the point and map created in the test are arbitrary. The point just has to be within the range of the map for the assertion to be true.