Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public interface IWebAPIAsync<t>
    {
        Task<List<t>> Load();
        Task Create(T obj);
        Task<t> Read(int key);
        Task Update(int key, T obj);
        Task Delete(int key);
    }
 public class WebAPITest<t>
    {
        #region Instance fields
        private IWebAPIAsync<t> _webAPI;

        #endregion

        #region Constructor

        public WebAPITest(IWebAPIAsync<t> webApi)
        {
            _webAPI = webApi;

        }

        public WebAPITest()
        {
        }
        #endregion

        #region Test methods for all five REST API methods
        public async Task RunAPITestLoad()
        {
            await LoadAndPrint();
        }

        public async Task RunAPITestCreate(T obj)
        {
            await RunAPITest(() => _webAPI.Create(obj));
        }

        public async Task RunAPITestRead(int key)
        {
            Task<t> readTask = _webAPI.Read(key);
            await readTask;
            PrintSingleRecord(readTask.Result);
        }

        public async Task RunAPITestUpdate(int key, T obj)
        {
            await RunAPITest(() => _webAPI.Update(key, obj));
        }

        public async Task RunAPITestDelete(int key)
        {
            await RunAPITest(() => _webAPI.Delete(key));
        }
        #endregion

        #region Private helper methods for test execution
        private async Task RunAPITest(Func<task> APIMethod)
        {

            await APIMethod();

        }

        private async Task LoadAndPrint()
        {
            Task<List<t>> loadTask = _webAPI.Load();
            await loadTask;
            PrintMultipleRecords(loadTask.Result);
        }

        private void PrintMultipleRecords(List<t> records)
        {

            foreach (var record in records)
            {
                Console.WriteLine(record);
            }

        }

        private void PrintSingleRecord(T record)
        {
            {
                Console.WriteLine(record);
            }
        }


    }
}


What I have tried:

var mock = new Mock<IWebAPIAsync<task1>>();
mock.Setup(x => x.Create(It.IsAny<task1>()));
mock.Verify();
Posted
Updated 13-May-18 22:37pm
v2
Comments
Patrice T 13-May-18 17:41pm    
What is the question ?
Member 13537727 13-May-18 18:16pm    
thanks for the comment, the question is how to test the methods create,load,read?
F-ES Sitecore 14-May-18 4:45am    
unit testing is generally to test business logic and processes, reading between the lines the code in your services is accessing\updating data in a database and you don't test those things via unit testing. To test they work you'd need access to a database, you'd need to do your Create and then have code to read the database to check the create worked, and then maybe delete the data too, but maybe the create added data to 20 tables and kicked off some other process via triggers?

1 solution

Mocking the Create method does not test the Create method. You mock your interface in order to test code that USES your interface, typically because your interface accesses a data store and you're writing unit tests, not integration tests
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900