我正在嘗試對類似于以下代碼的方法進行單元測驗:
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<outDto>()。這是清除物件初始化期間設定的值。
我想我可以回傳一個inDto帶有屬性集的,但是我真的在測驗代碼物件初始化代碼嗎?
我正在使用 xUnit 和 Moq。
uj5u.com熱心網友回復:
It.*僅用于設定模擬的期望。它不能用作變數。
如果您希望回傳傳遞的引數,則需要捕獲傳遞的引數,模仿預期的行為
//...
_converter
.Setup(_ => _.Convert(It.IsAny<OutDto>(), It.IsAny<Collection>()))
.Returns((OutDto d, Collection c) => {
//...do what ever modification (if ant) needed to be done to the captured dto
return d; //then return it
});
//...
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/491704.html
標籤:C# 单元测试 起订量 xunit xunit.net
上一篇:角度測驗-表單生效時啟用按鈕
