G Y

G Y

  • 1.5k
  • 229
  • 44.3k

ASP.NET CORE Unit testing using MS Unit Test Project Template

Jan 24 2022 12:27 PM

I have created a test class with test methods, it is working as expected

 

 [TestClass]
    public class EmployeeControllerTest
    {
        private EmployeesController _controller;

        public EmployeeControllerTest()
        {
            _controller = new EmployeesController(new SampleUnitTestProject.Models.SampleDBContext());
        }


        [TestMethod]
        public async Task CreateEmployeeTest()
        {
            Employee employee = new Employee
            {
                EmployeeId = 9,
                EmployeeName = "Prabhu",
                Salary = 40000
            };

            var id = await _controller.PostEmployee(employee);

            var dbId = await _controller.GetEmployee();

            Assert.AreEqual(employee, dbId.Value.ToList().Where(x => x.EmployeeId == employee.EmployeeId).Select(x => x).FirstOrDefault());
        }

        [TestMethod]
        public async Task PutEmployeeTest()
        {
            Employee employee = new Employee
            {
                EmployeeId = 8,
                EmployeeName = "Raj",
                Salary = 10000
            };

            var id = await _controller.PutEmployee(employee.EmployeeId, employee);

            var dbEmployee = await _controller.GetEmployee(); ;

            var result = dbEmployee.Value.ToList().Where(x => x.EmployeeId == employee.EmployeeId).Select(x => x).FirstOrDefault();

            Assert.AreEqual(employee, result);
        }

    }

 

 

But my requirement is to delete the newly created record created by using the create Test method after the test case is executed successfully.

The same has to be implemented for Put method and delete test methods also, after the test case method is executed successfully, the records has to be rollbacked.

Please assist me in achieving this


Answers (1)