Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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