使用Xunit测试时,如何在控制器中传递注射服务
我有DOT NET CORE(V5)Web API孔。
我想对测试我的Web API进行Xunit测试
。
我有一个用户controller
crud
。在我的构造函数中,我有4个输入,如下所示:
public UsersController(AppDbContext _appDbContext, IMapper _mapper, IOptions<SieveOptions> sieveOptions, SeiveProcessorService sieveProcessorService) : base(_appDbContext, _mapper)
{
_sieveProcessorService = sieveProcessorService;
_sieveOptions = sieveOptions.Value;
}
seiveProcessorService
在我的项目中其A Service
。
我可以在测试单元中传递三个输入,但我无法通过最后一个输入。
我不知道该如何发送到控制器
我的测试代码是:
[Fact]
public void Test_GetList()
{
using (var context = new AppDbContext(ContextOptions))
{
//auto mapper configuration
var mockMapper = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile(null));
});
var mapper = mockMapper.CreateMapper();
//
SieveOptions sieveOptions = new SieveOptions() {
CaseSensitive = false,
DefaultPageSize = 50,
MaxPageSize = 500,
ThrowExceptions = true,
};
IOptions<SieveOptions> options = Options.Create(sieveOptions);
// Arrange
var controller = new UsersController(context, mapper, options,????);
// Act
var result = controller.GetList(null);
// Assert
var viewResult = Assert.IsType<IResult>(result);
Assert.Null(viewResult);
}
}
}
我不知道如何发送第四个输入?
I have dot net core (v5) web api porject.
I want to make a Xunit test
for testing my web api.
I have a UsersController
for CRUD
. in my Constructor i have 4 input as shown below:
public UsersController(AppDbContext _appDbContext, IMapper _mapper, IOptions<SieveOptions> sieveOptions, SeiveProcessorService sieveProcessorService) : base(_appDbContext, _mapper)
{
_sieveProcessorService = sieveProcessorService;
_sieveOptions = sieveOptions.Value;
}
SeiveProcessorService
its a Service
in my project.
I can pass three input in my test unit but i can't pass last input.
i dont know how to send to controller
my test code is :
[Fact]
public void Test_GetList()
{
using (var context = new AppDbContext(ContextOptions))
{
//auto mapper configuration
var mockMapper = new MapperConfiguration(cfg =>
{
cfg.AddProfile(new MappingProfile(null));
});
var mapper = mockMapper.CreateMapper();
//
SieveOptions sieveOptions = new SieveOptions() {
CaseSensitive = false,
DefaultPageSize = 50,
MaxPageSize = 500,
ThrowExceptions = true,
};
IOptions<SieveOptions> options = Options.Create(sieveOptions);
// Arrange
var controller = new UsersController(context, mapper, options,????);
// Act
var result = controller.GetList(null);
// Assert
var viewResult = Assert.IsType<IResult>(result);
Assert.Null(viewResult);
}
}
}
I don't know how can send fourth input?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
创建
IseiveProcessorService
intererface和模拟
在单元测试中。更新
为了使用
MOQ
您需要安装和导入名称空间。在您的测试方法中:
Create
ISeiveProcessorService
intrerface andmock
it in your unit testing.UPDATE
in order to use
Moq
you need to install and import namespace.And in your test method: