Created
November 1, 2012 12:45
-
-
Save jsakamoto/3993430 to your computer and use it in GitHub Desktop.
How to write unit test code more simply.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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