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

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

What am I doing wrong?

I don't seem to figure out what am I doing wrong. Can anyone help me? Thanks!

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 x = 5;
            int y = 6;

            var map = new Map(x,y);
            var onMapX = OnMap(map.x);
            var onMapY = OnMap(map.y);

            Assert.Equal(x,onMapX);
            Assert.Equal(y,onMapY);

        }
    }
}
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

Steven Parker
Steven Parker
231,198 Points

The challenge reminds you that "OnMap returns true when passed a point that is on the map."

So you wouldn't want to perform an equality comparison on a coordinate dimension (like "x") with the result of calling OnMap (like "onMapX") since these will be different types. You also would not want to call OnMap with single dimension since it requires a Point object as an argument. And a Map object does not have "x" or "y" properties.

Instead, you might assert that calling OnMap with a valid map point returns true, and that calling it with a point that is not valid returns false.

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

Thank you sir! It's amazing (in a bad way) how sometimes I can't see the obvious thing staring me right in the face!