Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 9th, 2012  |  syntax: None  |  size: 3.15 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How can I avoid multiple asserts in this unit test?
  2. public class TestPoco
  3. {
  4.     public string StringValue { get; set; }
  5.     public int Int32Value { get; set; }
  6.     public bool BoolValue { get; set; }
  7. }
  8.        
  9. [Fact]
  10. public void TheTest()
  11. {
  12.     var input = new List<TestPoco>();
  13.     input.Add(new TestPoco { BoolValue = true, Int32Value = 1, StringValue = "foo" });
  14.  
  15.     var actual = input.ToRecordset();
  16.  
  17.     Assert.Equal(actual.BoolValue, true);
  18.     Assert.Equal(actual.Int32Value, 1);
  19.     Assert.Equal(actual.StringValue, "foo");
  20. }
  21.        
  22. // this probably doesn't actually compile, the actual conversion method
  23. // doesn't exist yet and this is just to show the idea
  24.  
  25. var expected = new ADODB.RecordsetClass();
  26. expected.Fields.Append("BoolValue", ADODB.DataTypeEnum.adBoolean);
  27. expected.Fields.Append("Int32Value", ADODB.DataTypeEnum.adInteger);
  28. expected.Fields.Append("StringValue", ADODB.DataTypeEnum.adVarWChar);
  29.  
  30. expected.AddNew();
  31. expected.BoolValue = true;
  32. expected.Int32Value = 1;
  33. expected.StringValue = "foo";
  34. expected.Update();
  35.        
  36. public class ClassWithProperities
  37. {
  38.     public string Foo { get; set; }
  39.     public int Bar { get; set; }
  40. }
  41.  
  42. public static class Converter
  43. {
  44.     public static ClassWithProperities Convert(string foo, int bar)
  45.     {
  46.         return new ClassWithProperities {Foo=foo, Bar=bar};
  47.     }
  48. }
  49. [TestClass]
  50. public class PropertyTestsWhenFooIsTestAndBarIsOne
  51. {
  52.     private static ClassWithProperities classWithProperties;
  53.  
  54.     [ClassInitialize]
  55.     public static void ClassInit(TestContext testContext)
  56.     {
  57.         //Arrange
  58.         string foo = "test";
  59.         int bar = 1;
  60.         //Act
  61.         classWithProperties = Converter.Convert(foo, bar);
  62.         //Assert
  63.     }
  64.  
  65.     [TestMethod]
  66.     public void AssertFooIsTest()
  67.     {
  68.         Assert.AreEqual("test", classWithProperties.Foo);
  69.     }
  70.  
  71.     [TestMethod]
  72.     public void AssertBarIsOne()
  73.     {
  74.         Assert.AreEqual(1, classWithProperties.Bar);
  75.     }
  76. }
  77.  
  78. [TestClass]
  79. public class PropertyTestsWhenFooIsXyzAndBarIsTwoThousand
  80. {
  81.     private static ClassWithProperities classWithProperties;
  82.  
  83.     [ClassInitialize]
  84.     public static void ClassInit(TestContext testContext)
  85.     {
  86.         //Arrange
  87.         string foo = "Xyz";
  88.         int bar = 2000;
  89.         //Act
  90.         classWithProperties = Converter.Convert(foo, bar);
  91.         //Assert
  92.     }
  93.  
  94.     [TestMethod]
  95.     public void AssertFooIsXyz()
  96.     {
  97.         Assert.AreEqual("Xyz", classWithProperties.Foo);
  98.     }
  99.  
  100.     [TestMethod]
  101.     public void AssertBarIsTwoThousand()
  102.     {
  103.         Assert.AreEqual(2000, classWithProperties.Bar);
  104.     }
  105. }
  106.        
  107. public class When_converting_a_TestPoco_to_Recordset
  108. {
  109.     protected static List<TestPoco> inputs;
  110.     protected static Recordset actual;
  111.  
  112.     Establish context = () => inputs = new List<TestPoco> { new TestPoco { /* set values */ } };
  113.  
  114.     Because of = () => actual = input.ToRecordset ();
  115.  
  116.     It should_have_copied_the_bool_value = () => actual.BoolValue.ShouldBeTrue ();
  117.     It should_have_copied_the_int_value = () => actual.Int32Value.ShouldBe (1);
  118.     It should_have_copied_the_String_value = () => actual.StringValue.ShouldBe ("foo");
  119. }
  120.        
  121. Assert.That (actual.BoolValue == true && actual.Int32Value == 1 && actual.StringValue == "foo");