Skip to content

Instantly share code, notes, and snippets.

@jsakamoto
Created November 1, 2012 12:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsakamoto/3993430 to your computer and use it in GitHub Desktop.
Save jsakamoto/3993430 to your computer and use it in GitHub Desktop.
How to write unit test code more simply.
// PM> Install-Package ChainingAssertion
// http://nuget.org/packages/ChainingAssertion
// http://chainingassertion.codeplex.com/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class DateHelper
{
public static DateTime? ToNullableDate(string input)
{
if (string.IsNullOrWhiteSpace(input)) return null;
DateTime d;
if (DateTime.TryParse(input, out d)) return d;
throw new ArgumentException();
}
}
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
DateHelper.ToNullableDate("2012/03/01").Is(new DateTime(2012, 3, 1));
DateHelper.ToNullableDate("").IsNull();
AssertEx.Throws<ArgumentException>(() => DateHelper.ToNullableDate("invalidValue"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment