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();
}
如果“view”是一个模拟,那么所有测试都会调用一个模拟对象。模拟对象通常用于删除外部依赖关系。你究竟想要测试什么? – PatrickSteele
您可以发布您的所有代码(TestInitialize)或至少在您创建视图对象的位置吗?您的测试方法还应包含调用非模拟代码的代码。你的测试真的是这一行吗? – Gilles