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

Unit Test C# Task 1

Keeps coming back with an error. What's wrong?

[Fact] public void OnMapTest() { int width = 5; int height =5;

        var mp = new Map(width,height)

        int x = 2;
        int y = 2;

        var pt = new Point(x,y);                    

        Assert.Equals(true, mp.OnMap(pt));
    }
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 = 5;
            int height =5; 

            var mp = new Map(width,height)

            int x = 2;
            int y = 2;

            var pt = new Point(x,y);                    

            Assert.Equals(true, mp.OnMap(pt));
        }
    }
}
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
229,732 Points

I see two issues:

  • you're missing the semicolon (";") at the end of the "var mp = ..." line.
  • you spelled "Equals" (with an "s") instead of "Equal".

But while you can pass the challenge by just fixing the errors, I believe the challenge intended for you to use the Assert.True that was provided initially (but replacing the false of course). Assert.True is a better test for something that returns a boolean than comparing it to true.