c# - moq No setups configured error,如何快速正确添加setup

标签 c# visual-studio unit-testing moq mstest

我有一个最小起订量的单元测试,其中我收到一个错误,我需要进行设置,我确实看到了示例,但一切都如此不同,以至于我想进行适当的最小起订量设置

错误:“预期至少对模拟调用一次,但从未执行过:调用 => called.SetWidgetResponseResponseData("searchType", It.IsAny()) 没有配置任何设置。”

所有这 3 个测试都失败了

[TestMethod]
public void it_should_call_set_response_response_data_on_the_view_reccount()
{
   view.Verify(called => called.SetWidgetResponseResponseData("recCount",It.IsAny<string>()));
}

[TestMethod]
public void it_should_call_set_response_response_data_on_the_view_searchtype()
{
  view.Verify(called => called.SetWidgetResponseResponseData("searchType", It.IsAny<string>()));
}

[TestMethod]
public void it_should_call_set_response_response_data_on_the_view_isgeosearch()
{
  view.Verify(called => called.SetWidgetResponseResponseData("isGEOSearch", It.IsAny<bool>()));
}

我认为这个错误是因为“res”需要用最小起订量模拟,对吗?

if (res != null && res.Count > 0)
{ 

    View.SetWidgetResponseResponseData("recCount", res.Count.ToString());
    View.SetWidgetResponseResponseData("searchType", provFacSearchCrt.SearchType);
    View.SetWidgetResponseResponseData("isGEOSearch", provFacSearchCrt.IsGeoSearch);
}

更新

   public abstract class ProviderSearchPresenterContext :   Specification<Tc.Cbc.Presentation.ProviderSearchPresenter>
{
    protected Mock<ICESBaseWidgetView> view = new Mock<ICESBaseWidgetView>();
    protected Mock<ILookupServiceManager> lookupService = new Mock<ILookupServiceManager>(MockBehavior.Loose);
    protected Mock<ICAPProviderService> capProvider = new Mock<ICAPProviderService>(MockBehavior.Loose);
    protected Mock<IProviderFacilityServiceManager> prvFacServiceMgr = new Mock<IProviderFacilityServiceManager>(MockBehavior.Loose);
    //protected Mock<ProviderFacilitySearchCriteria> provFacSearchCrt = new Mock<ProviderFacilitySearchCriteria>(MockBehavior.Loose);
    protected Mock<ICESTraceManager> traceManager = new Mock<ICESTraceManager>();
    protected Mock<ILogger> logger = new Mock<ILogger>();
    protected Mock<IResourcesHelper> resources = new Mock<IResourcesHelper>();
    protected Mock<IUserContext> userContext = new Mock<IUserContext>();

    protected NameValueCollection QueryString = new NameValueCollection();
    protected NameValueCollection Form = new NameValueCollection();
    protected Dictionary<string, string> MethodArguments = new Dictionary<string, string>();

    protected override Tc.CES.Presentation.ProviderSearchPresenter construct()
    {
        //capProvider.Setup(x => x.GetProvider(It.Is<GetProviderReqMsg>(y => y.GetProvider.ProviderSystemIDs[0].SystemIDName == CESConstants.PROVIDER_ID 
        //    && y.GetProvider.ProviderSystemIDs[0].SystemIDValue == CESConstants.TZCOMMON))).Returns(new GetProviderRespMsg { 

        var presenter = new Tc.CES.Presentation.ProviderSearchPresenter(view.Object, traceManager.Object, logger.Object, resources.Object, 
            userContext.Object, lookupService.Object, capProvider.Object, prvFacServiceMgr.Object);
        presenter.QueryString = QueryString;
        presenter.Form = Form;
        presenter.MethodArguments = MethodArguments;
        return presenter;
    }

    protected override void given() { }

    protected override void when()
    {
        QueryString["ProFacSearch"] = "FACILITY";
        sut.ShowProviderSearch(null, null);
    }
}

规范类如下所示:

[TestClass]
public abstract class Specification<SUT>
{
    protected SUT sut;

    [TestInitialize]
    public void Initialize()
    {
        sut = construct();

        given();

        when();
    }

    protected abstract SUT construct();
    protected abstract void given();
    protected abstract void when();
}

最佳答案

好的,我修好了。我添加了这样的设置:

this.prvFacServiceMgr.Setup(call =>     call.SearchProviderFacility(It.IsAny<ProviderFacilitySearchCriteria>())).Returns(new List<ProviderFacilitySearchResult>() 
        { 
            new ProviderFacilitySearchResult()
            { 
                ProviderName="TestProvider"
            } 
        });

关于c# - moq No setups configured error,如何快速正确添加setup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8346995/

相关文章:

C# 多个字符串包含

c# - 如何使用正则表达式拆分字符串以返回值列表?

c# - 求一个Winforms System.ComponentModel.Design Design Surface BeginDrag/EndDrag事件钩子(Hook)

visual-studio - c使SOURCE_GROUP多个文件?

java - 如何使用 Mockito 捕获中间值?

javascript - 模拟 React.Suspense 同时保持 React 的其余部分完好无损

c# - 容器 UserControl - 处理和修改添加的控件

c# - Blazor 链接 - 如果有 onclick 方法,则禁用 href

c++ - 由于 show 函数导致的非法间接错误

c# - AspNetCore 上的单元测试 Controller 模型验证