How can I pass dynamic objects into an NUnit TestCase function?

后端 未结 4 675
忘了有多久
忘了有多久 2020-12-12 22:10

I am writing a data-intensive application. I have the following tests. They work, but they\'re pretty redundant.

[Test]
public void DoSanityCheck_WithCountEqu         


        
4条回答
  •  -上瘾入骨i
    2020-12-12 22:30

     [Test]
     [TestCase("textbox", true, "Text is empty", null, false)]
     [TestCase("textbox", false, "Text is empty", null, true)]
     public void Test_Component_Validation_and_ValidationText__Whether_IsMandatory_IsSet(string textbox, bool isMandatory, string validationText, string value, bool expectedValue)
     {
         // Arrange
         var mockPublicPortalService = new Mock();
         PublicAssessmentController controller = new PublicAssessmentController(mockPublicPortalService.Object);
    
         // Set Component properties
         var Component = new Component()
         {
             ComponentDatatype = textbox,
             IsMandatory = isMandatory,
             ValidationText = validationText,
             Value = value
         };
    
         var context = new ValidationContext(Component);
    
         // Act
         var results = new List();
         var isModelStateValid = Validator.TryValidateObject(Component, context, results, true);
    
         // Assert
         Assert.AreEqual(expectedValue, isModelStateValid);
         if (isModelStateValid == false)
         {
             Assert.IsTrue(results.Any(x => x.ErrorMessage == validationText));
         };
     }
    

提交回复
热议问题