Skip to content

Instantly share code, notes, and snippets.

@MattKeen
Created August 19, 2014 07:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattKeen/361124ab4a2c101edcd1 to your computer and use it in GitHub Desktop.
Save MattKeen/361124ab4a2c101edcd1 to your computer and use it in GitHub Desktop.
[TestClass]
public class GravityTest
{
private const decimal EarthFactor = 9.8M;
private IPlanet Planet { get; set; }
private Gravity Target { get; set; }
[TestInitialize]
public void BeforeEachTest()
{
Planet = Mock.Create<IPlanet>();
Target = new Gravity(Planet);
}
[TestClass]
public class GetVelocityAfter : GravityTest
{
[TestMethod]
public void Returns_Zero_When_Passed_Zero()
{
AssertVelocityAfterNumberOfSeconds(numberOfSeconds: 0, expectedVelocity: 0);
}
[TestMethod]
public void Returns_Nine_Point_Eight_After_1_Second_On_Earth()
{
Planet.Arrange(p => p.GetGravitationalFactor()).Returns(EarthFactor);
AssertVelocityAfterNumberOfSeconds(numberOfSeconds: 1, expectedVelocity: 9.8);
}
[TestMethod]
public void Returns_NinetyEight_After_10_Seconds_On_Earth()
{
Planet.Arrange(p => p.GetGravitationalFactor()).Returns(EarthFactor);
AssertVelocityAfterNumberOfSeconds(numberOfSeconds: 10, expectedVelocity: 98);
}
private void AssertVelocityAfterNumberOfSeconds(int numberOfSeconds, decimal expectedVelocity)
{
var velocityAfterGivenSeconds = Target.GetVelocityAfter(numberOfSeconds);
Assert.AreEqual<decimal>(expectedVelocity, velocityAfterGivenSeconds);
}
}
}
@rhughesjr
Copy link

I think an assertion library like Should or Fluent Assertions can help to achieve the readability you are trying to get to without over-refactoring it:

using FluentAssertions;

[TestClass]
public class GravityTest
{
    private const decimal EarthFactor = 9.8M;
    private IPlanet Planet { get; set; }
    private Gravity Target { get; set; }

    [TestInitialize]
    public void BeforeEachTest()
    {
        Planet = Mock.Create<IPlanet>();
        Target = new Gravity(Planet);
    }

    [TestClass]
    public class GetVelocityAfter : GravityTest
    {
        [TestMethod]
        public void Returns_Zero_When_Passed_Zero()
        {
            var velocityAfterGivenSeconds = Target.GetVelocityAfter(0);

            velocityAfterGivenSeconds.Should().Be(0);
        }

        [TestMethod]
        public void Returns_Nine_Point_Eight_After_1_Second_On_Earth()
        {
            Planet.Arrange(p => p.GetGravitationalFactor()).Returns(EarthFactor);

            var velocityAfterGivenSeconds = Target.GetVelocityAfter(1);

            velocityAfterGivenSeconds.Should().Be(9.8);
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment