Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created September 12, 2012 19: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 akimboyko/3709463 to your computer and use it in GitHub Desktop.
Save akimboyko/3709463 to your computer and use it in GitHub Desktop.
NUnit Exception Testing
void Main()
{
// nunit runner
NUnit.ConsoleRunner.Runner.Main(new string[]
{
Assembly.GetExecutingAssembly().Location,
});
}
public interface IBookMarket
{
}
public class BooksInfo
{
}
public class BookInfoProvider
{
public BooksInfo GetBookInfo(IBookMarket bookinfo)
{
if(bookinfo == null)
throw new ArgumentNullException("bookinfo should not be null");
return null;
}
}
[TestFixture]
public class TestGetBookInfo
{
[Test]
[ExpectedException(
ExpectedException = typeof(ArgumentNullException),
ExpectedMessage = "bookinfo should not be null",
MatchType = MessageMatch.Contains)]
public void TestGetBookInfoExceptionOldStyle()
{
new BookInfoProvider().GetBookInfo(null);
}
[Test]
public void TestGetBookInfoExceptionNewStyle()
{
Assert.That(
() => new BookInfoProvider().GetBookInfo(null),
Throws.InstanceOf<ArgumentNullException>()
.And.Message.Contains("bookinfo should not be null"));
}
object[] TestData =
{
new TestCaseData(new BookMarketStub()), // "good" case
new TestCaseData(null).Throws(typeof(ArgumentNullException)) // "bad" exceptional case
};
[Test]
[TestCaseSource("TestData")]
public void TestGetBookInfoDataDriven(IBookMarket bookinfo)
{
new BookInfoProvider().GetBookInfo(bookinfo);
Assert.Pass("all ok"); // this is not necessary
}
}
public class BookMarketStub : IBookMarket
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment