C# lambda表达式中的替换方法

C# lambda表达式中的替换方法,c#,lambda,delegates,C#,Lambda,Delegates,我正在为在单个接口上定义的一系列类似方法编写单元测试(这是重构的前身)。我发现类似的代码出现在多种方法中: // Each line here shows up in a different test method. mock.Setup(m => m.MethodA()).Returns(() => true); mock.Setup(m => m.MethodB()).Returns(() => true); mock.Setup(m => m.MethodC(

我正在为在单个接口上定义的一系列类似方法编写单元测试(这是重构的前身)。我发现类似的代码出现在多种方法中:

// Each line here shows up in a different test method.
mock.Setup(m => m.MethodA()).Returns(() => true);
mock.Setup(m => m.MethodB()).Returns(() => true);
mock.Setup(m => m.MethodC()).Returns(() => true);
我想创建一个单一的方法,我可以通过该方法进行测试,但我不知道如何做到这一点。我想要这样的东西:

// testMethod is some method defined on IMyInterface.
private Mock<IMyInterface> SetupMockObject(Func<bool> testMethod)
{
    var mock = new Mock<MyInterface>();
    mock.Setup(m => m.testMethod()).Returns(() => true);
    return mock;
}
这可能吗

编辑:表达式树似乎正是我所需要的-之前我没有得到可以传递lambda表达式的概念。我在下面粘贴了解决方案,因为我花了点时间才弄明白这一点,尽管dBurner和Servy的答案都证明是有用的

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Moq.Language.Flow;
using System;
using System.Linq.Expressions;

namespace ExampleTest
{
    [TestClass]
    public class TestClass
    {
        private Mock<IMyInterface> _mock;

        private interface IMyInterface
        {
            bool MethodA();
            bool MethodB();
        }

        private ISetup<IMyInterface, bool> SetupMockObject(Expression<Func<IMyInterface,bool>> lambda)
        {
            var mockSetup = _mock.Setup(lambda);
            return mockSetup;
        }

        [TestInitialize]
        public void Setup()
        {
            _mock = new Mock<IMyInterface>();
        }

        [TestMethod]
        public void TestMethodA()
        {
            SetupMockObject(i => i.MethodA()).Returns(true);     
            // Proceed with act and assert.
        }

        [TestMethod]
        public void TestMethodB()
        {
            SetupMockObject(i => i.MethodB()).Returns(true);
            // Proceed with act and assert.
        }
    }
}
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用最小起订量;
使用Moq.Language.Flow;
使用制度;
使用System.Linq.Expressions;
名称空间示例测试
{
[测试类]
公共类TestClass
{
私人模拟;
专用接口IMyInterface
{
布尔方法a();
bool方法b();
}
专用ISetup SetupMockObject(表达式lambda)
{
var mockSetup=_mock.Setup(lambda);
返回模拟设置;
}
[测试初始化]
公共作废设置()
{
_mock=新mock();
}
[测试方法]
公共void TestMethodA()
{
SetupMockObject(i=>i.MethodA()).Returns(true);
//继续行动和主张。
}
[测试方法]
公共void TestMethodB()
{
SetupMockObject(i=>i.MethodB())。返回(true);
//继续行动和主张。
}
}
}

您可以为此使用表达式树。您的
CommonTest
方法应该有一个
表达式
参数


使用此参数类型,可以获取方法名称。在此之后,您应该构造另一个表达式来表示
m=>m.MethodFromParameter

您可以为此使用表达式树。您的
CommonTest
方法应该有一个
表达式
参数


使用此参数类型,可以获取方法名称。在此之后,您应该构造另一个表达式来表示
m=>m.MethodFromParameter

您可以为此使用表达式树。您的
CommonTest
方法应该有一个
表达式
参数


使用此参数类型,可以获取方法名称。在此之后,您应该构造另一个表达式来表示
m=>m.MethodFromParameter

您可以为此使用表达式树。您的
CommonTest
方法应该有一个
表达式
参数


使用此参数类型,可以获取方法名称。在此之后,您应该构造另一个表达式来表示
m=>m.MethodFromParameter

传递给它的lambda需要有一个模拟类型的参数,而当前参数接受一个无参数委托:

public static Mock<MyInterface> SetupMockObject<T>(
    Func<MyInterface, bool> testMethod)
{
    var mock = new Mock<MyInterface>();
    mock.Setup(testMethod).Returns(() => true);
    return mock;
}
代码是否与

var mock = new Mock<MyInterface>();
mock.Setup(m => m.MethodA()).Returns(() => true);
var mock=new mock();
mock.Setup(m=>m.MethodA())。返回(()=>true);

您传递给它的lambda需要具有模拟类型的参数,而您的当前参数接受无参数委托:

public static Mock<MyInterface> SetupMockObject<T>(
    Func<MyInterface, bool> testMethod)
{
    var mock = new Mock<MyInterface>();
    mock.Setup(testMethod).Returns(() => true);
    return mock;
}
代码是否与

var mock = new Mock<MyInterface>();
mock.Setup(m => m.MethodA()).Returns(() => true);
var mock=new mock();
mock.Setup(m=>m.MethodA())。返回(()=>true);

您传递给它的lambda需要具有模拟类型的参数,而您的当前参数接受无参数委托:

public static Mock<MyInterface> SetupMockObject<T>(
    Func<MyInterface, bool> testMethod)
{
    var mock = new Mock<MyInterface>();
    mock.Setup(testMethod).Returns(() => true);
    return mock;
}
代码是否与

var mock = new Mock<MyInterface>();
mock.Setup(m => m.MethodA()).Returns(() => true);
var mock=new mock();
mock.Setup(m=>m.MethodA())。返回(()=>true);

您传递给它的lambda需要具有模拟类型的参数,而您的当前参数接受无参数委托:

public static Mock<MyInterface> SetupMockObject<T>(
    Func<MyInterface, bool> testMethod)
{
    var mock = new Mock<MyInterface>();
    mock.Setup(testMethod).Returns(() => true);
    return mock;
}
代码是否与

var mock = new Mock<MyInterface>();
mock.Setup(m => m.MethodA()).Returns(() => true);
var mock=new mock();
mock.Setup(m=>m.MethodA())。返回(()=>true);


不清楚您的意思-您如何称呼它?设置的参数是什么类型的?如果它是一个表达式而不是func,那应该是可能的。@JonSkeet我假设
CommonTest(m=>m.MethodA())
或者类似的东西(可能在循环中)@DStanley:可能是这样-但目前还不清楚,老实说,不清楚你的意思-你怎么称呼它?设置的参数是什么类型的?如果它是一个表达式而不是func,那应该是可能的。@JonSkeet我假设
CommonTest(m=>m.MethodA())
或者类似的东西(可能在循环中)@DStanley:可能是这样-但目前还不清楚,老实说,不清楚你的意思-你怎么称呼它?设置的参数是什么类型的?如果它是一个表达式而不是func,那应该是可能的。@JonSkeet我假设
CommonTest(m=>m.MethodA())
或者类似的东西(可能在循环中)@DStanley:可能是这样-但目前还不清楚,老实说,不清楚你的意思-你怎么称呼它?设置的参数是什么类型的?如果它是一个表达式而不是func,那应该是可能的。@JonSkeet我假设
CommonTest(m=>m.MethodA())
或者类似的东西(可能在循环中)@DStanley:可能是这样,但老实说,目前还不清楚。花点时间阅读帮助中心的。堆栈溢出上的格式设置与其他站点不同。你需要对内联代码使用反勾号,否则编辑器会认为它是html并将其删除。我在手机上写了这个,我打算编辑它:)这不是一个好方法,因为它的方式、方式、比你需要做的更多的工作,也是一个完全不完整的答案,因为你们甚至还并没有讨论过要使用它需要做的大量工作