Is there any data mocking for calling Web API Put and post methods with C# Code
-
Hi, I have the following Web Api methods, I want to call and test my methods from a mocking application, if possible any code, following are my Web api methods:
\[Produces("application/json")\] \[Route("api/Employee")\] public class EmployeeController : Controller { EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer(); \[HttpGet("\[action\]")\] //\[Route("api/Employee/Index")\] public IEnumerable Index() { return objemployee.GetAllEmployees(); } \[HttpPost\] public int Create(TblEmployee employee) { return objemployee.AddEmployee(employee); } \[HttpGet("{id}")\] //\[HttpGet("{id}", Name = "Details")\] public TblEmployee Details(int id) { return objemployee.GetEmployeeData(id); } \[HttpPut("{id}")\] public int Edit(int id, TblEmployee employee) { return objemployee.UpdateEmployee(employee); } \[HttpDelete("{id}")\] public int Delete(int id) { return objemployee.DeleteEmployee(id); } \[HttpGet("\[action\]")\] public IEnumerable GetCityList() { return objemployee.GetCities(); } }
-
Hi, I have the following Web Api methods, I want to call and test my methods from a mocking application, if possible any code, following are my Web api methods:
\[Produces("application/json")\] \[Route("api/Employee")\] public class EmployeeController : Controller { EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer(); \[HttpGet("\[action\]")\] //\[Route("api/Employee/Index")\] public IEnumerable Index() { return objemployee.GetAllEmployees(); } \[HttpPost\] public int Create(TblEmployee employee) { return objemployee.AddEmployee(employee); } \[HttpGet("{id}")\] //\[HttpGet("{id}", Name = "Details")\] public TblEmployee Details(int id) { return objemployee.GetEmployeeData(id); } \[HttpPut("{id}")\] public int Edit(int id, TblEmployee employee) { return objemployee.UpdateEmployee(employee); } \[HttpDelete("{id}")\] public int Delete(int id) { return objemployee.DeleteEmployee(id); } \[HttpGet("\[action\]")\] public IEnumerable GetCityList() { return objemployee.GetCities(); } }
Mocking in tests is to replace the access of external resources with other things like hard-coded values so that you can test the business logic in your app independent of other things. So the short answer is that you would probably mock the entire web EmployeeController so that any code that calls the webapi would call your mocked controller instead. If it is the controller itself you want to test then you would mock out the data access, in this case your EmployeeDataAccessLayer, so you could call the methods on your controller from a unit test without needing database access. However your controller methods don't really do anything, there is no logic in them, they just act as an interface to calls on the data access layer. So long story short, there is nothing here worth testing.
-
Hi, I have the following Web Api methods, I want to call and test my methods from a mocking application, if possible any code, following are my Web api methods:
\[Produces("application/json")\] \[Route("api/Employee")\] public class EmployeeController : Controller { EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer(); \[HttpGet("\[action\]")\] //\[Route("api/Employee/Index")\] public IEnumerable Index() { return objemployee.GetAllEmployees(); } \[HttpPost\] public int Create(TblEmployee employee) { return objemployee.AddEmployee(employee); } \[HttpGet("{id}")\] //\[HttpGet("{id}", Name = "Details")\] public TblEmployee Details(int id) { return objemployee.GetEmployeeData(id); } \[HttpPut("{id}")\] public int Edit(int id, TblEmployee employee) { return objemployee.UpdateEmployee(employee); } \[HttpDelete("{id}")\] public int Delete(int id) { return objemployee.DeleteEmployee(id); } \[HttpGet("\[action\]")\] public IEnumerable GetCityList() { return objemployee.GetCities(); } }
-
Hi, I have the following Web Api methods, I want to call and test my methods from a mocking application, if possible any code, following are my Web api methods:
\[Produces("application/json")\] \[Route("api/Employee")\] public class EmployeeController : Controller { EmployeeDataAccessLayer objemployee = new EmployeeDataAccessLayer(); \[HttpGet("\[action\]")\] //\[Route("api/Employee/Index")\] public IEnumerable Index() { return objemployee.GetAllEmployees(); } \[HttpPost\] public int Create(TblEmployee employee) { return objemployee.AddEmployee(employee); } \[HttpGet("{id}")\] //\[HttpGet("{id}", Name = "Details")\] public TblEmployee Details(int id) { return objemployee.GetEmployeeData(id); } \[HttpPut("{id}")\] public int Edit(int id, TblEmployee employee) { return objemployee.UpdateEmployee(employee); } \[HttpDelete("{id}")\] public int Delete(int id) { return objemployee.DeleteEmployee(id); } \[HttpGet("\[action\]")\] public IEnumerable GetCityList() { return objemployee.GetCities(); } }
When we are using Microsoft Test runner we use Moq to build out our mock data layer. We recently have been switching to xUnit, I haven't tried using Moq with it yet. Here is the link to the Moq GitHub repo, there's good info here.