Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have performed integration Testing on my feature but the problem is when I get the data normally from database it works as expected but when I create mock objects for testing purpose that method
GetDetailedSAPInfoById
returns null you can see that in the code

What I have tried:

Here is my testing method
public async Task GenerateSapInfoReport_MustReturnPathOfCreatedExcel()
       {

           var apiAccess = await GetAccessToken();
           _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiAccess);
           var httpResponse = await _client.GetAsync(SapInfoExportEndpoint);//URL
           httpResponse.EnsureSuccessStatusCode();
           // Deserialize and examine results.
           var viewModel = await GetViewModel<SapInfoExportViewModel>(httpResponse);
           Assert.NotNull(viewModel);
           Assert.NotNull(viewModel.RelativePath);

       }


The class where I provide the mock object to the test method
    public class DbInit
    {
        private static string _email = UserConstants.Email;
        public static void Seed(SHIELDDbContext context)
        {
            
            AddSapInfoExportTestingObjects(sapRepo);
        }

        
        private async static void AddSapInfoExportTestingObjects(SAPRepository<SAPInfo> saprepo)
        {
            var checklist = new List<CheckItem>()
            {
                new CheckItem{CheckDescription="abc", CheckGroup=1, Result=0, },
                new CheckItem{CheckDescription="abc", CheckGroup=1, Result=0, },
                new CheckItem{CheckDescription="abc", CheckGroup=1, Result=0, }
            };
            var issues = new List<Issue>()
            {
                new Issue{ IssueCategory=IssueCategory.noproblem, IssueType=67, Description="blah"},
                new Issue{ IssueCategory=IssueCategory.noproblem, IssueType=67, Description="blah"},
                new Issue{ IssueCategory=IssueCategory.issue, IssueType=65, Description="blah"}
            };
            var defaultprofile = new List<ProfileParameter>
            {
                new ProfileParameter {ParameterName="a",ParameterValue="hhh"},
                new ProfileParameter {ParameterName="a",ParameterValue="hhh"},
                new ProfileParameter {ParameterName="a",ParameterValue="hhh"},
            };
            var Instance = new List<Instance>()
            {
                new Instance{ Id=1, }

            };
            var InstanceProfile = new List<InstanceProfile>()
            {
                new InstanceProfile{ Id=1, ProfileData="abc", InstanceId=1},
                new InstanceProfile{ Id=2, ProfileData="abc", InstanceId=2},
                new InstanceProfile{ Id=3 , ProfileData="abc", InstanceId=3},
            };
            var sapDB = new SAPDB { DBSchema = "SAPSR3", DBType = "2", Server = "k-p83-db" };
            var sapInfo = new SAPInfo
            {
                SID = SapInfoConstants.Sid,
                Section = SapInfoConstants.Section,
                SAPDB = sapDB,
                CheckList = checklist,
                Issues = issues,
                LastCheck = DateTime.Now,
                Instances = Instance,
                _DefaultProfile = defaultprofile,
                



            };
            
            
             saprepo.Add(sapInfo);
        }

    }
}


The below line returns null when debug the testing method otherwise it returns the data
var sapInfo = await _sapRepository.GetDetailedSAPInfoById(request.Id);

public Task<T> GetDetailedSAPInfoById(int id)
{

    return _dbContext.Set<T>()
                   .Where(t => t.Id == id)
                     .Include(i => i.Instances)
                            .ThenInclude(instance => instance.KernelInfo)
                        .Include(i => i.Instances)
                            .ThenInclude(instance => instance.ServerNetwork)
                                .ThenInclude(server => server.VirtualNames)
                        .Include(i => i.Instances)
                            .ThenInclude(instance => instance.InstanceProfile)
                        .Include(i => i.Instances)
                            .ThenInclude(instance => instance.DbClients)
                        .Include(i => i.Instances)
                            .ThenInclude(instance => instance.SSLCerts)
                        .Include(i => i.Issues)
                        .Include(i => i.SAPDB)
                        .Include(i => i.DefaultProfile)
                        .Include(i => i.CheckList)
                        .FirstOrDefaultAsync();


}
Posted

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