How to use ClassData attribute using Xunit to arrange data dynamically

26 Views Asked by At

I'm writing unit tests using Xunit / AutoFixture / Moq frameworks using C#. I have around 10 scenarios to unit test with different inputs and outputs. That means in the arrange phase, I have to arrange and hard code all the input and outputs for different scenarios. I found that we can use ClassData attribute to achieve this in a better way. Can anyone guide me how can I dynamically add different data for different scenarios. Here are my classes and service. I need to test EmpService-> GetEmployeeInfo method and have to around 10 unit tests with different inputs/outputs

      public class EmployeeOutput
      {
        public long EmpId { get; set;}
        public double EmployeeWorkedHours {get;set;}
        public bool IsEmpQualifyForBonus {get;set;}
      }
    public class EmployeeInput
     {
       public long EmpId { get; set; }
       public string EmpAddress { get; set; }
       public int Age {get;set;}
       public float Salary {get;set;}
       public string EmployeeType { get; set; }
      public int EmpRoleId { get; set; }
    }

     public class EmpService
     {
        public async Task<EmployeeOutput> GetEmployeeInfo (EmployeeInput ip)
         {
     
         }
     }
public class EmpServiceTest
         {
          private readonly Mock<DbContext> _context;
          private readonly Fixture _fixture;
          public EmpServiceTest()
            {
            _context = new Mock<DbContext>();
            _fixture = new Fixture();
            }

         [Fact]
          public async Task Emp_Test()
             {
              //Arrange
           var employeeOutput = _fixture.Create<EmployeeOutput>();
           employeeOutput.EmpId = 100;
           employeeOutput.EmployeeWorkedHours = 40;
           employeeOutput.IsEmpQualifyForBonus = true;

           var employeeInput = _fixture.Create<EmployeeInput>();
           employeeInput.EmpId = 100;
           employeeInput.EmpAddress = "NY";
       employeeInput.EmpAddress = 30;
       employeeInput.Salary = 10000;
       employeeInput.EmployeeType = "Contract";
       employeeInput.EmpRoleId = 20;
          var empService = new EmpService(_context.Object);

          //Act

          await empService.GetEmployeeInfo(shiftDetailsInput);

         //Assert
         }
    }

Can anyone guide me how can I arrange data dynamically using MemberData/ClassData attributes for different scenarios using different inputs and outputs for employeeOutput and employeeInput classes

0

There are 0 best solutions below