我的问题是模拟对象不返回它返回null的值
我的MSpec UnitTest如下
public class With_Fake_Data_Service
{
protected static Mock<IMProposalWCFService> _fakeDataService;
protected static FaultContract fault;
private Establish context = () =>
{
_fakeDataService = new Mock<IMProposalWCFService>();
_fakeDataService.Setup(
service =>
service.ReplyToProposal(new ReplyToProposalRequest(Moq.It.IsAny<Proposal>(), Moq.It.IsAny<bool>())))
.Returns(new ReplyToProposalResponse( Moq.It.IsAny<bool>(), fault));
_fakeDataService.Setup(
service => service.ReplyToProposalEmail(new ReplyToProposalEmailRequest(Moq.It.IsAny<string>(), Moq.It.IsAny<bool>())))
.Returns(new ReplyToProposalEmailResponse(Moq.It.IsAny<string>(), fault));
_fakeDataService.Setup(service => service.GetAllProposals(Moq.It.IsAny<GetAllProposalsRequest>()))
.Returns(() => new GetAllProposalsResponse(new List<Proposal>(){new Proposal()}, fault));
_fakeDataService.Setup(service => service.GetAllProposals(Moq.It.IsAny<GetAllProposalsRequest>())).Verifiable();
};
}
public class When_Testing_HomeController_Index : With_Fake_Data_Service
{
protected static HomeController _homeController;
protected static IList<Proposal> _proposals;
private Establish context = () =>
{
_homeController = new HomeController(_fakeDataService.Object);
};
private Because of = () =>
{
var result = _homeController.Index() as ViewResult;
_proposals = result.Model as IList<Proposal>;
};
private It Should_Have_Called_GetAllProposals_At_Least_Once =
() => _fakeDataService.Verify(service => service.GetAllProposals(Moq.It.IsAny<GetAllProposalsRequest>()), Times.Exactly(1));
}
实际代码内部HomeController索引方法在单元测试
上运行时,我得到响应为null public HomeController(IMProposalWCFService service)
{
_service = service;
}
public ActionResult Index()
{
var response = _service.GetAllProposals(new GetAllProposalsRequest());
if (response.fault == null) /*Right Here reponse is null when running unit test*/
{
var proposals = response.GetAllProposalsResult;
}
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
return View();
}
为什么我没有得到无效的回应,换句话说为什么我没有得到
new GetAllProposalsResponse(new List(){new Proposal()},fault)
请帮助
答案 0 :(得分:0)
发现问题
public class With_Fake_Data_Service
{
protected static Mock<IMProposalWCFService> _fakeDataService;
protected static FaultContract fault;
private Establish context = () =>
{
_fakeDataService = new Mock<IMProposalWCFService>();
_fakeDataService.Setup(
service =>
service.ReplyToProposal(new ReplyToProposalRequest(Moq.It.IsAny<Proposal>(), Moq.It.IsAny<bool>())))
.Returns(new ReplyToProposalResponse( Moq.It.IsAny<bool>(), fault));
_fakeDataService.Setup(
service => service.ReplyToProposalEmail(new ReplyToProposalEmailRequest(Moq.It.IsAny<string>(), Moq.It.IsAny<bool>())))
.Returns(new ReplyToProposalEmailResponse(Moq.It.IsAny<string>(), fault));
_fakeDataService.Setup(service => service.GetAllProposals(Moq.It.IsAny<GetAllProposalsRequest>()))
.Returns(() => new GetAllProposalsResponse(new List<Proposal>(){new Proposal()}, fault)).Verifiable();
};
}