public class crumbController: Controller
{
public ActionResult<IEnumerable<Crumb>> GetBreadcrumb([FromRoute]int tag_X_ParentID)
{
try
{
Services.Interface.IBreadcrumbService service = new Services.Imp.BreadcrumbService(_configuration, _logger);
if (service != null)
{
Breadcrumb breadcrumb = service.GetBreadcrumb(ParentID);
return Ok(breadcrumb.crumbs.ToList());
}
else
{
return BadRequest("Unable to establish connection.");
}
}
catch (Exception ex)
{
_logger.LogError(ex.Message, ex);//log error
return BadRequest(ex.Message);
}
}
}
public class crumbControllerShould
{
[Fact]
public void GetBreadCrumbsTest()
{
IConfiguration _configuration;
IHostingEnvironment _env;
ILoggerManager _logger;
int tag_X_ParentID = 12;
var mock_logger = new Mock<ILoggerManager>();
_logger = mock_logger.Object;
var mock_configuration = new Mock<IConfiguration>();
_configuration = mock_configuration.Object;
var mock_env = new Mock<IHostingEnvironment>();
mock_env
.Setup(m => m.EnvironmentName)
.Returns("http://localhost:8980/");
_env = mock_env.Object;
Assert.IsAssignableFrom<IHostingEnvironment>(_env);
Assert.IsAssignableFrom<IConfiguration>(_configuration);
Assert.IsAssignableFrom<ILoggerManager>(_logger);
var controller = new BreadcrumbController(_configuration, _env, _logger);
var result = controller.GetBreadcrumb(tag_X_ParentID);
Assert.NotNull(result);
Assert.IsType<IEnumerable<Crumb>>(result);
//this line fails indicating that expected: System.Collections.Generic.IEnumerable'1[[FHH.crumpApi.Entitities.crumb, FHH.BreadcrumbApi]]
//Actual: Microsoft.AspNetCore.Mvc.ActionResult'1 [[System.Collections.Generic.IEnumerable'1[[FHH.crubmApi.Entities.crumb,FHH.crumbapi]]
}
}
;
请查看解释问题的代码中的注释行。
另外,我需要获取对象才能进行比较Assert.Equal(2,item.tagID)。
如果我解决问题,将其分配给名为actionResult的变量
然后将其分配给var item = actionResult.firstOrDefault()以获取item.tagID。
我正在该网站上跟踪以下示例进行IEnumerable单元测试。谢谢您的反馈。
答案 0 :(得分:0)
您需要获取ActionResult的模型值才能执行预期的操作。您可以通过将ActionResult转换为ViewResult并获取模型值来实现。有关更多详细信息,请参阅this question的可接受答案。