Moq的可链接实现

时间:2011-10-21 03:21:10

标签: moq method-chaining

那里有Moq的可链接实现吗?我想的不是这个:

var mockSchedule = new Mock<Schedule>();
mockSchedule.SetupGet(x => x.Date).Returns(new DateTime(2011,6,1));
mockSchedule.SetupGet(x => x.Label).Returns("Schedule A");

我可以这样称呼它:

var mockSchedule = 
    new Mock<Schedule>()
        .Which().SetupGet(x => x.Date).Returns(new DateTime(2011,6,1))
        .Which().SetupGet(x => x.Label).Returns("Schedule A");

或者像这样:

var mockSchedule =
    new Mock<Schedule>().
        .SetupGetWith(x => x.Date,new DateTime(2011,6,1))
        .SetupGetWith(x => x.Label,"Schedule A");

我可以自己写这些,但如果有现有的实现,我宁愿不重新发明轮子

1 个答案:

答案 0 :(得分:4)

排序;有Moq v4功能规范。

var foo = Mock.Of<IFoo>(f =>
    f.Id == 1 &&
    f.Who == "me" &&
    f.GetBar(It.IsAny<string>()) == Mock.Of<IBar>(
        b => b.Name == "Fred"));

文档可能更好。我有一个short writeup on my blog。另请参阅Old style imperative mocks vs moq functional specificationsthis Moq Discussions thread