我正在尝试编写以下if语句的模拟测试:
if (Application.GetGroup("Hospital").GetEntity(i).GetField("hosp_id").GetInternalValue() == "Y")
{
//some code.
}
我试过以下:
var mockHosp = MockRepository.GenerateMock<IEntity>();
hospGrp.Stub(x => x.GetEntity(0)).Return(mockHosp);
var hosp_id = MockRepository.GenerateMock<IField>();
hospGrp.Stub(x => mockHosp.GetField("hosp_id")).Return(hosp_id);
hosp_id.Stub(x => x.Value).Return("Y");
我一直在这条线上获得InvalidOperationException
:
hospGrp.Stub(x => mockHosp.GetField("hosp_id")).Return(hosp_id);
这可以实现吗?
答案 0 :(得分:0)
鉴于GetField方法似乎属于IEntity接口,您应该将其存储在
之下mockHosp.Stub(x => x.GetField("hosp_id")).Return(hosp_id);
答案 1 :(得分:0)
我终于明白了。在这里提出解决方案可能会帮助某人:
var mockHosp = MockRepository.GenerateMock<IEntity>();
hospGrp.Stub(x => x.GetEntity(0)).Return(mockHosp);
mockHosp.Stub(x => x.Id).Return("1");
var hosp_id = MockRepository.GenerateMock<IField>();
hospGrp.Stub(x => x.GetField("hosp_id")).Return(hosp_id);
hosp_id.Stub(x => x.Value).Return("Y");