gpt4 book ai didi

c# - 使用最小起订量测试此代码的正确方法是什么?

转载 作者:行者123 更新时间:2023-11-30 22:29:03 25 4
gpt4 key购买 nike

我有以下方法:

    public void MoveChannelUp(string channelName)
{
var liveChannels = _repository.GetChannels<LiveChannel>();

var channels = GetModifiedChannelsList(channelName, liveChannels);

_repository.SaveChannels(channels);
}

我想对 SaveChannels() 调用设置一个预期,以便传入正确的 channel 参数。

我试过了:

   channelsRepository.Setup(x => x.SaveChannels(reorderedChannels));

reorderedChannels 是我期望 GetModifiedChannelsList() 调用将返回的位置,但我得到模拟验证异常(可能是由于 reorderedChannels 与 channel 不是同一个对象???)

所以我真正想测试的是 GetModifiedChanneslsList()(我知道我可以使用反射来测试它)

那么我该如何测试传递给 SaveChannels() 的正确 channel 列表?

最佳答案

你可以这样做(我假设有一个名为 Channel 的类型并且 SaveChannels 的参数是 List<Channel> ;替换为实际的):

var expectedChannels = new List<Channel> { new Channel() }; // set up expected channels here

var channelsRepo = new Mock<IChannelsRepository>();

// perform your unit test using channelsRepo here, for example:

channelsRepo.Object.SaveChannels(new List<Channel> { new Channel() });

channelsRepo.Verify(x => x.SaveChannels(It.Is<List<Channel>>(l => l.SequenceEqual(expectedChannels)))); // will throw an exception if call to SaveChannels wasn't made, or the List of Channels params did not match the expected.

此代码的作用是验证 SaveChannels使用正确的 channel 列表至少调用一次方法。如果那没有发生,Verify将抛出异常,您的单元测试将按预期失败。

关于c# - 使用最小起订量测试此代码的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10395528/

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