Advertisement
akimboyko

NUnit Exception Testing

Aug 19th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. void Main()
  2. {
  3.     // nunit runner
  4.     NUnit.ConsoleRunner.Runner.Main(new string[]
  5.     {
  6.         Assembly.GetExecutingAssembly().Location,
  7.     });
  8. }
  9.  
  10. public interface IBookMarket
  11. {
  12. }
  13.  
  14. public class BooksInfo
  15. {
  16. }
  17.  
  18. public class BookInfoProvider
  19. {
  20.     public BooksInfo GetBookInfo(IBookMarket bookinfo)
  21.     {
  22.         if(bookinfo == null)
  23.             throw new ArgumentNullException("bookinfo should not be null");
  24.            
  25.         return null;
  26.     }
  27. }
  28.  
  29. [TestFixture]
  30. public class TestGetBookInfo
  31. {
  32.     [Test]
  33.     [ExpectedException(
  34.         ExpectedException = typeof(ArgumentNullException),
  35.         ExpectedMessage = "bookinfo should not be null",
  36.         MatchType = MessageMatch.Contains)]
  37.     public void TestGetBookInfoExceptionOldStyle()
  38.     {
  39.         new BookInfoProvider().GetBookInfo(null);
  40.     }
  41.    
  42.     [Test]
  43.     public void TestGetBookInfoExceptionNewStyle()
  44.     {
  45.         Assert.That(
  46.             () => new BookInfoProvider().GetBookInfo(null),
  47.                 Throws.InstanceOf<ArgumentNullException>()
  48.                     .And.Message.Contains("bookinfo should not be null"));
  49.     }
  50.  
  51.     object[] TestData =
  52.     {
  53.         new TestCaseData(new BookMarketStub()), // "good" case
  54.         new TestCaseData(null).Throws(typeof(ArgumentNullException)) // "bad" exceptional case
  55.     };
  56.  
  57.     [Test]
  58.     [TestCaseSource("TestData")]
  59.     public void TestGetBookInfoDataDriven(IBookMarket bookinfo)
  60.     {
  61.         new BookInfoProvider().GetBookInfo(bookinfo);
  62.         Assert.Pass("all ok"); // this is not necessary
  63.     }
  64. }
  65.  
  66. public class BookMarketStub : IBookMarket
  67. {
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement