Moq的快速模板?

时间:2014-02-25 09:43:04

标签: resharper moq template-engine

手动制作许多Moq的Setup()调用是非常常规的,以便模拟一整套接口方法。我想找到一种方法来预先填充模拟表达式,并使用虚拟响应。

有没有办法这样做?如果相关,我会使用ReSharper。

2 个答案:

答案 0 :(得分:1)

如果您使用的是Visual Studio,则可以创建代码段,例如:

moqq

然后当您使用 moqq + TAB + TAB 时,它会显示如下:

Mock<Interface> mock = new Mock<Interface>();
mock.Setup(x => x.MyMethod(params)).Return(return);

你可以做所有这些只写moqq。

即使您可以在代码段中添加一些“点”,然后当您再次点击TAB时,您将例如从Interface传递给MyMethod。这样您甚至不需要触摸鼠标来更改所有变量名称;)

按照:

http://msdn.microsoft.com/en-us/library/ms165393(v=vs.90).aspx

答案 1 :(得分:0)

使用基于 Oscar's answer 的 Resharper“实时模板”:

Mock<$interfaceToMock$> mock$interfaceToMock$ = new Mock<$interfaceToMock$>();
mock$interfaceToMock$.Setup(x => x.$expectedMethod$).Returns($returnValue$);

包含宏等的整个 Resharper DotSettings 内容(基于 R# 2020.3):

<wpf:ResourceDictionary xml:space="preserve"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:s="clr-namespace:System;assembly=mscorlib"
                        xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml"
                        xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Shortcut/@EntryValue">moqq</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Description/@EntryValue">Moq mock</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Text/@EntryValue">Mock&lt;$interfaceToMock$&gt; mock$interfaceToMock$ = new Mock&lt;$interfaceToMock$&gt;();&#xD; mock$interfaceToMock$.Setup(x =&gt; x.$expectedMethod$).Returns($returnValue$);</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Reformat/@EntryValue">True</s:Boolean>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/ShortenQualifiedReferences/@EntryValue">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Categories/=Tests/@EntryIndexedValue">Tests</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Applicability/=Live/@EntryIndexedValue">True</s:Boolean>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Scope/=C3001E7C0DA78E4487072B7E050D86C5/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Scope/=C3001E7C0DA78E4487072B7E050D86C5/Type/@EntryValue">InCSharpFile</s:String>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Scope/=C3001E7C0DA78E4487072B7E050D86C5/CustomProperties/=minimumLanguageVersion/@EntryIndexedValue">2.0</s:String>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=interfaceToMock/@KeyIndexDefined">True</s:Boolean>
    <s:String x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=interfaceToMock/Expression/@EntryValue">guessExpectedType("")</s:String>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=interfaceToMock/InitialRange/@EntryValue">2</s:Int64>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=interfaceToMock/Order/@EntryValue">0</s:Int64>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=expectedMethod/@KeyIndexDefined">True</s:Boolean>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=expectedMethod/Order/@EntryValue">1</s:Int64>
    <s:Boolean x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=returnValue/@KeyIndexDefined">True</s:Boolean>
    <s:Int64 x:Key="/Default/PatternsAndTemplates/LiveTemplates/Template/=E53EF07BEAF6964C83AD78E1B75E3837/Field/=returnValue/Order/@EntryValue">2</s:Int64>
</wpf:ResourceDictionary>

Gist