我如何测试也通过方法重新调整的实例化类?
我正在尝试单元测试类似于以下代码的方法:
public OutDto ToOutDto(InDto inDto)
{
var outDto = new OutDto
{
Property1 = inDto.Property2
//More mapping here
};
outDto = _converter.ConvertCollection(outDto, inDto.Collection);
return outDto;
}
问题是呼叫到_Converter.convertCollection
正在替换呼叫上方创建的OUTDTO
。
到目前为止,这是我的单位测试:
var inDto = new IntDto()
{
Property2 = "Property",
Collection = new Collection()
};
_converter.Setup(t => t.Convert(It.IsAny<outDto>(), intDto.Collection)).Returns(It.IsAny<outDto>());
var result = _sut.ToDto(inDto);
Assert.Equal(inDto.Property2, result.Property1);
_converter.Verify(t => t.Convert(It.IsAny<outDto>(), inDto.Collection), Times.Once());
我认为,问题是我正在返回it.isany&lt; oftto&gt;()
。正在清理对象初始化期间设置的值。
我想我可以在属性设置的情况下返回indoto
的,但是我真的在测试代码对象初始化代码吗?
我正在使用Xunit和ior。
I am trying to unit test a method similar to the below code:
public OutDto ToOutDto(InDto inDto)
{
var outDto = new OutDto
{
Property1 = inDto.Property2
//More mapping here
};
outDto = _converter.ConvertCollection(outDto, inDto.Collection);
return outDto;
}
The problem is the call out to _converter.ConvertCollection
is replacing the outDto
created above the call.
Here is my Unit Test so far:
var inDto = new IntDto()
{
Property2 = "Property",
Collection = new Collection()
};
_converter.Setup(t => t.Convert(It.IsAny<outDto>(), intDto.Collection)).Returns(It.IsAny<outDto>());
var result = _sut.ToDto(inDto);
Assert.Equal(inDto.Property2, result.Property1);
_converter.Verify(t => t.Convert(It.IsAny<outDto>(), inDto.Collection), Times.Once());
The problem is, I think, I am returning It.IsAny<outDto>()
. Which is cleaning out the values set during the object initialization.
I guess I could return an of inDto
with the properties set, but then I am really testing the code object initialization code?
I am using xUnit and Moq.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
it。*
仅是指设置模拟的期望。它不应用作变量。如果您希望将其归还,则需要捕获通过的参数,模仿预期的行为
It.*
is only mean to be used in setting up expectations of mocks. It is not to be used as a variable.You will need to capture the passed argument if it is you want it to be be returned, mimicking the expected behavior