C# Unit Testing in C# Test Driven Development Green

Daniel Hildreth
Daniel Hildreth
16,170 Points

Not sure what is wrong with my unit tests

Can someone please help me determine what is wrong with my unit tests? I seemed to have follow the video to the "T", but I am getting 3 tests failing. Why is this happening?

Here is my GitHub with my project in it: https://github.com/HanSolo0001/Unit-Testing

Here is the link to the video where I noticed the errors: https://teamtreehouse.com/library/green

1 Answer

Justin Kraft
Justin Kraft
26,327 Points

Hello Daniel,

When I cloned your repository to my local workstation I noticed within Point.cs you did not override the Equals() and GetHashCode() methods in the base object class, therefore the object comparison in your tests were failing. They should be overridden similar to the code snippet shown below.

public override bool Equals(object obj) {
    Point other = obj as Point;

    if(other == null)
        return false;

    return X == other.X && Y == other.Y;
}

public override int GetHashCode() {
    return X.GetHashCode() + Y.GetHashCode() * 31;
}

After adding the two methods to the Point class, the tests pass as expected.

Daniel Hildreth
Daniel Hildreth
16,170 Points

Actually, I noticed what's wrong with my code. I thought the final code download under the Teacher's Notes were where we were going to be coding along with Jeremy. However, that is not the case. Is there a copy and paste, or a download for all the code he added into the video? Or am I going to have to try and pause it and write it all out?

Edited: Ok, I noticed why his code was "different" than mine. He was in the Point class, whereas I was in the PointTest class. Silly mistake on my end, but fixed it.