Moq的可链接实现(Chainable Implementation for Moq)

编程入门 行业动态 更新时间:2024-10-24 02:32:29
Moq的可链接实现(Chainable Implementation for Moq)

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");

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

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

最满意答案

有点; 有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"));

文档可能会更好。 我的博客上有一篇简短的文章 。 另请参阅旧式命令式模拟与moq功能规范以及此Moq讨论主题 。

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.

更多推荐

本文发布于:2023-08-07 19:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465606.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   Moq   Implementation   Chainable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!