gpt4 book ai didi

c# - .NET CORE MVC ViewComponent xUnit 测试

转载 作者:太空宇宙 更新时间:2023-11-03 12:18:20 25 4
gpt4 key购买 nike

<分区>

我已经开始将 xUnit 用于示例 .NET CORE MVC 项目,并且我正在努力向 ViewComponent 添加测试,其中包括 IOptionsIHostingEnvironment。这是一个从 appsettings.json 文件返回值的简单 View 组件,它本身可以正常工作。

appsettings.json 片段:

"Application": {"Name": "My App","Version": "1.0.0","Author": "John Doe", "Description": "Just a template!"}

View 组件:

[ViewComponent(Name = "Footer")]
public class FooterViewComponent : ViewComponent
{
private readonly IOptions<AppSettings.Application> _app;
private readonly IHostingEnvironment _env;

public FooterViewComponent(IOptions<AppSettings.Application> app, IHostingEnvironment env)
{
_app = app;
_env = env;
}

public IViewComponentResult Invoke()
{
var vm = new FooterViewModel();
{
vm.AppName = _app.Value.Name;
vm.AppVersion = _app.Value.Version;
vm.AppEnvironment = _env.EnvironmentName;
}

return View(vm);
}
}

我想测试返回类型是 ViewComponent 结果并且 View Model 不为空。

ViewComponent 测试:

public class FooterViewComponentTest
{
public class Should
{
[Fact]
public void ReturnViewCompnentWithViewModel()
{
// Arrange
var viewComp = new FooterViewComponent(??????????);

// Act
var result = viewComp ??????????;

// Assert
Assert.IsType<ViewComponentResult>(result);

}
}
}

我仍在努力解决这个问题,并将根据我的发现编辑我的片段。有人有什么建议吗?我应该用这种格式编写测试吗?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com