Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chainable Implementation for Moq

Is there a chainable implementation of Moq out there? I was thinking that instead of this:

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

I can call it like this:

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

or like this:

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

I could write these myself but if there's an existing implementation I'd rather not reinvent the wheel

like image 914
Jonn Avatar asked Feb 17 '26 13:02

Jonn


1 Answers

Sort of; there's the Moq v4 functional specifications.

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

The documentation could be better. I've got a short writeup on my blog. See also Old style imperative mocks vs moq functional specifications and this Moq Discussions thread.

like image 57
TrueWill Avatar answered Feb 21 '26 13:02

TrueWill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!