gpt4 book ai didi

c# - 预期对模拟调用一次,但为 0 次 : No setups configured?

转载 作者:行者123 更新时间:2023-11-30 12:38:23 24 4
gpt4 key购买 nike

我使用 Moq 进行了以下 xunit 测试。

[Fact]
public void Presenter_Filter_ShouldFilterViewSearchList()
{
var mockView = Mock.Get(mockSearchView);
mockView.Setup(v => v.Ids).Returns("123");
presenter.Filter();
var mockRepo = Mock.Get(mockSearchRepository);
Filter filter = new Filter { Ids = new List<string> { "123" } };
mockRepo.Verify(r => r.GetSearchItems(5000, filter), Times.Once);
}

测试失败并显示以下消息。

Expected invocation on the mock once, but was 0 times:     r => r.GetSearchItems(5000, Filter)No setups configured.    Performed invocations: ISearchRepository.GetSearchItems(5000)ISearchRepository.GetSearchItems(5000, Filter)

The following is the tested function. And ISearchRepository.GetSearchItems(5000, Filter) is called?

public void Filter()
{
var filter = new Filter {
Name = _view.Name,
Ids = _view.Ids?.Split(',').Select(x => x.Trim()).ToList(),
Countries = _view.Countries?.Split(',').Select(x => x.Trim()).ToList(),
Region = _view.Region,
};
_repository.GetSearchItems(5000, filter);
}

最佳答案

您的函数正在创建自己的过滤器:

var filter = new Filter {
Name = _view.Name,
Ids = _view.Ids?.Split(',').Select(x => x.Trim()).ToList(),
Countries = _view.Countries?.Split(',').Select(x => x.Trim()).ToList(),
Region = _view.Region,
};

您正在使用不同的过滤器实例设置测试:

Filter filter = new Filter { Ids = new List<string> { "123" } };
mockRepo.Verify(r => r.GetSearchItems(5000, filter), Times.Once);

如果要测试调用,传递的对象必须是同一个对象。

否则,将其配置为使用任何 Filter 对象进行调用:

 mockRepo.Verify(r => r.GetSearchItems(5000, It.IsAny<Filter>()), Times.Once);

或者将过滤器作为参数传递给函数:

public void Filter(Filter filter) 
{
_repository.GetSearchItems(5000, filter);
}

关于c# - 预期对模拟调用一次,但为 0 次 : No setups configured?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53070343/

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