mardi 18 février 2020

Cannot instantiate proxy of class error: Cannot instantiate proxy of class error. How to fix it?

I am facing highlighted issue while writing test cases with xUnit and Moq in .Net Core I looked into the error, but am not able to understand how to fix the issue and the Assert statement?

Test Method

 public async void GetDropDownList_knownReportName_ReturnsFoundResult()
    {
        Mock<IDataSourceService> mock = new Mock<IDataSourceService>();
        QueryService service = new QueryService(mock.Object);

        DataController controller = new DataController(service);
        var user = new User() { ConexusId = "ConexusId", Name = "Name", Role = "Role", SalesForceUserId = "SalesForceUserId", SalesForceUserName = "SalesForceUserId", Token = "Token", CustomerName = "MTPA" };

        var user1 = new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
        {
           new Claim(ClaimTypes.Name,user.Name),
           new Claim(ClaimTypes.GroupSid,user.CustomerName),
           new Claim(ClaimTypes.Role,user.Role)
        }, "mock"));

        controller.ControllerContext = new ControllerContext()
        {
            HttpContext = new DefaultHttpContext() { User = user1 }
        };
        var okResult = await controller.GetDropDownList("MTPAAccountReport", "profileList");

        Assert.IsType<DropDownList>(okResult);
    }

Query Service

 public async Task<DropDownList> GetDropDownList(string reportName, string queryName, ClaimResult claims)
    {
        var report = ReportConfig.Reports.FirstOrDefault(s => s.ReportName == reportName);
        if (report == null)
            throw new Exception(string.Format("Query Not found for reportName:{0}, queryName:{1}", reportName, queryName));
        if (report.CustomerName != claims.GroupSid)
            throw new Exception($"Customer {claims.GroupSid} is not allowed to access this database");
        var query = report.Queries.FirstOrDefault(s => s.Name == queryName);
        if (query == null)
            throw new Exception(string.Format("Query Not found for reportName:{0}, queryName:{1}", reportName, queryName));
        if(query.Type != QueryType.DropDown)
            throw new Exception(string.Format("Query is not drop down type for reportName:{0}, queryName:{1}", reportName, queryName));
        if (report.DataSourceType == DataSourceType.SqlServer)
        {
            var sqlQuery = DataSourceService.GetSqlQueryServiceByName(report.DataSourceName);
            return await sqlQuery.GetDropDownList(reportName, queryName, query.QueryText);
        }
        else if (report.DataSourceType == DataSourceType.SalesForce)
        {
            var soqlQuery = DataSourceService.GetSalesForceQueryService(report.DataSourceName);
            if (!string.IsNullOrEmpty(query.EntityName) && !string.IsNullOrEmpty(query.ColumnName))
                return await soqlQuery.GetDropDownList(reportName, queryName, query.EntityName, query.ColumnName);
            else
                throw new Exception($"Query {queryName} not configured correctly");
        }
        else
            throw new Exception(string.Format("Datasource type not supported for reportName:{0}, queryName:{1}", reportName, queryName));
    }

Line I am getting error at:

if (report.DataSourceType == DataSourceType.SqlServer)
            {
                **var sqlQuery = DataSourceService.GetSqlQueryServiceByName(report.DataSourceName);**//error
                return await sqlQuery.GetDropDownList(reportName, queryName, query.QueryText);
            }

GetSqlServiceByName Method:(This method i Have written into the DataSourceService)

 public SqlQueryService GetSqlQueryServiceByName(string dataSourceName)
    {
        var connection = DataSources.SqlDataSources.FirstOrDefault(s => s.DataSourceName == dataSourceName);
        return new SqlQueryService(connection);
    }

Aucun commentaire:

Enregistrer un commentaire