Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Is there any data mocking for calling Web API Put and post methods with C# Code

Is there any data mocking for calling Web API Put and post methods with C# Code

Scheduled Pinned Locked Moved ASP.NET
jsoncsharpdatabase
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simpledeveloper
    wrote on last edited by
    #1

    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();
        }
    }
    
    F J 3 Replies Last reply
    0
    • S simpledeveloper

      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();
          }
      }
      
      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • S simpledeveloper

        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();
            }
        }
        
        J Offline
        J Offline
        James Walsh Jr
        wrote on last edited by
        #3

        xUnit unit testing framework has the ability to use external data sources for unit testing. For example you could create a spreadsheet or a database table will a randomized set of data that can be used as the mocked source data for your unit testing. Here is a link to a blog post discussing it.

        1 Reply Last reply
        0
        • S simpledeveloper

          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();
              }
          }
          
          J Offline
          J Offline
          James Walsh Jr
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups