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

Seb Psky
PLUS
Seb Psky
Courses Plus Student 4,966 Points

I am confused about the following code, can someone assist?

Can someone explain this and the answer code to me.

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()
        { 
            int Width=1;
           int Height=1;
           new point(Width,Height);

            Map.OnMap(1);
            Assert.True(Map.OnMap);
        }
    }
}
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));
        }
    }
}

2 Answers

andren
andren
28,558 Points

I'll first post the answer and then I'll try to provide some explanation.

MapTest.cs:

using Xunit;

namespace TreehouseDefense.Tests
{
    public class MapTests
    {
        [Fact]
        public void OnMapTest()
        {
            Map map = new Map(10,10); // Create an instance of Map
            Point point = new Point(8, 8); // Create an instance of Point
            Assert.True(map.OnMap(point), "This test needs an implementation"); // Call OnMap and pass in the point
        }
    }
}

The Assert.True method tests if the thing you pass in as the first argument returns true. It it does then the test passes, if it does not the test fails. The point of this test is to see if passing a point that is on the map actually results in the OnMap method returning true like it is supposed to.

To do that you need the same thing you would need to use the OnMap method in the real application, which is an instance of Map, and an instance of Point. Since the point is to test that OnMap can correctly identify a Point that is on the Map you should create a Map and Point that fit each other.

Then all you have to do is call the OnMap method and pass it in as an argument to Assert.True. Since OnMap returns a boolean on its own you don't have to compare it to anything in order for the Assert.True method to evaluate whether it worked or not.

Seb Psky
Seb Psky
Courses Plus Student 4,966 Points

This makes a lot of sense once you understand the application. Thank you.

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

I had the same issue. The problem with this challenge is that it uses a new method from the xUnit.net without proper introduction. First, after watching the previous tutorial, I used the "Assert.Equals()" method and it did not notice the "Assert.True()" method that was provided by the challenge.

Some of the challenges (from the C# part of the tutorials) have really ambiguous instructions and most you have to seak help on this forum, in order to successfully complete the challenge. This is not cool, Treehouse!