Advertisement
apieceoffruit

Nested Tests

May 1st, 2021 (edited)
1,035
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using FluentAssertions;
  5. using NUnit.Framework;
  6. using ResponseSerialization;
  7. using ResponseSerialization.Exceptions;
  8.  
  9. namespace ResponseSerializationTests
  10. {
  11.     [TestFixture]
  12.     public class ResponseSerializer_Should
  13.     {
  14.         public class When_Deserializing
  15.         {
  16.             [SetUp]
  17.             public void Setup() => _serializer = new ResponseSerializer();
  18.  
  19.             [TestCaseSource(nameof(NullAndEmptyValues))]
  20.             public void Empty_Values_Throws_NullOrEmpty(string input)
  21.             {
  22.                 Action act = () => _serializer.Deserialize<object>(input);
  23.                act.Should().Throw<NullOrEmpty>();
  24.             }
  25.  
  26.             [TestCaseSource(nameof(NotJson))]
  27.             public void NotJson_Throws_NotJson(string input)
  28.             {
  29.                 Action act = () => _serializer.Deserialize<object>(input);
  30.                 act.Should()
  31.                     .Throw<NotJson>()
  32.                     .Where(x=>x.Content.Equals(input));
  33.             }
  34.  
  35.             [Test]
  36.             public void KeyWithoutQuotes_Succeeds()
  37.             {
  38.                 var result = _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_NonStringKey);
  39.                 result.Id.Should().Be("Test");
  40.             }
  41.  
  42.             [Test]
  43.             public void ModelMismatch_Throws_ModelMismatch()
  44.             {
  45.                 Action act = () => _serializer.Deserialize<WrongModel>(JsonExamples.JsonObject_NonStringKey);
  46.                 act.Should().Throw<ModelMismatch>().WithMessage("*.WrongModel*");
  47.             }
  48.  
  49.             [Test]
  50.             public void Array_WhenExpectedSingle_Throws_UnexpectedArray()
  51.             {
  52.  
  53.                 Action act = () => _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_Array);
  54.                 act.Should().Throw<UnexpectedArray>().WithMessage("*.CorrectModel*");
  55.             }
  56.  
  57.             [Test]
  58.             public void Single_WhenExpectingArray_Throws_UnexpectedObject()
  59.             {
  60.                 Action act = () => _serializer.Deserialize<List<CorrectModel>>(JsonExamples.JsonObject_NonStringKey);
  61.                 act.Should().Throw<UnexpectedObject>().WithMessage("*.CorrectModel*");
  62.             }
  63.            
  64.             [Test]
  65.             public void Malformed_Throws_NotJson()
  66.             {
  67.                 Action act = () => _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_Malformed);
  68.                 act.Should().Throw<NotJson>();
  69.             }
  70.  
  71.             [Test]
  72.             public void Valid_Json_Deserializes()
  73.             {
  74.                 var result = _serializer.Deserialize<CorrectModel>(JsonExamples.JsonObject_NonStringKey);
  75.                 result.Id.Should().Be("Test");
  76.             }
  77.  
  78.             [Test]
  79.             public void List_Deserializes()
  80.             {
  81.                 List<CorrectModel> results = _serializer.Deserialize<List<CorrectModel>>(JsonExamples.JsonObject_Array);
  82.                 results.Count.Should().Be(4);
  83.                 results.First().Id.Should().Be("A");
  84.             }
  85.            
  86.            
  87.             static string[] NotJson = {"<html><title>SomeHtmlValue</title></html>", "JustARandomString", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><note><to>RandomXML</to></note>"};
  88.            
  89.             static string[] NullAndEmptyValues = {"", null};
  90.             ResponseSerializer _serializer;
  91.         }
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement