我如何测试也通过方法重新调整的实例化类?

发布于 2025-02-06 21:34:51 字数 988 浏览 2 评论 0原文

我正在尝试单元测试类似于以下代码的方法:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2025-02-13 21:34:51

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
    }); 

//...

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

//...

_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
    }); 

//...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文