Home > Net >  I'm trying to create MSTest method for .NET Core application
I'm trying to create MSTest method for .NET Core application

Time:03-23

[TestMethod]
[DataRow("connectionstringvalue",
"[{'ColumName':'neid','Value':'1','Operator':'Equal'}]",
"neid",
Sort.ASC,

what value should be given here for model binding. PaginationDto in the method parameter is >model class having page and recordsperpage properties

) ]

public void MethodTest(string connectionString,string? filters, string sortColumn, Sort sort, PaginationDto? pagination)
{
var serviceProvider = new ServiceCollection()
.AddLogging()
.BuildServiceProvider();



var mediator = new Mock<IMediator>();



var factory = serviceProvider.GetService<ILoggerFactory>();



var logger = factory.CreateLogger<HController>();



HController controller = new HController (logger, **mediator.Object**, _configuration);
Task<ActionResult<List<basicDto>>> result = controller.methodname(connectionString, filters, sortColumn, sort, pagination);

// 1. how to handle the value returned from my controller Assert.AreEqual("", "Hi! Reader"); }

1.how to handle the value returned from my controller

2.how to pass input value for model class property

CodePudding user response:

Since attributes only allow constant expressions, you cannot use DataRow here (as I assume that PaginationDto is actually not an enum, but a class or struct).

You can, however, use DynamicData. In "short" it works like this:

[TestClass]
public class Test
{
   [TestMethod]
   [DynamicData(nameof(MethodTestData), DynamicDataSourceType = DynamicDataSourcetype.Property)]
   public void Method(string connectionString,
       string? filters, string sortColumn, Sort sort, 
       PaginationDto? pagination)
   {
        // test code
   }

   // The property must be public, static and have return type IEnumerable<object[]>
   public static IEnumerable<object[]> MethodTestData
   {
       get
       {
            // Return one array/set of arguments per call of Method()
            yield return new object[]
            { 
               // each item of the array is a parameter (value) to Method()
               "the connection string",
               "the filters",
               "the sort column",
               Sort.Asc,
               new PaginationDto()
            };
       }
   }

}

As for your other questions, I suggest you post separate questions for them. One hint, you can also have async test methods (if your controller method is also async).

  • Related