2011-12-01 67 views
2

我有一個單元測試與moq在其中我得到一個錯誤,我需要做一個設置,我看到的例子,但一切都如此不同,我我想要做適當的AA設置MOQ錯誤moq沒有設置配置錯誤,如何快速添加設置

:「在模擬預期調用至少一次,但從未進行:叫=> called.SetWidgetResponseResponseData(」搜索類型」,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>())); 
} 

我認爲錯誤是因爲「資源」需要被嘲笑起訂量與用設置嗎?

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(); 
} 
+1

如果「view」是一個模擬,那麼所有測試都會調用一個模擬對象。模擬對象通常用於刪除外部依賴關係。你究竟想要測試什麼? – PatrickSteele

+1

您可以發佈您的所有代碼(TestInitialize)或至少在您創建視圖對象的位置嗎?您的測試方法還應包含調用非模擬代碼的代碼。你的測試真的是這一行嗎? – Gilles

回答

3

好吧,我固定它。我添加了一個像這樣的設置:

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