c#-Moq具体的类方法调用

我有一个这样的设置,带有一个具体的类,该类在要测试的方法中实例化.我想模拟这个具体的类,不要让它执行内部代码.因此,不应引发任何异常:

public class Executor
{
    public bool ExecuteAction(ActionRequest request)
    {
        switch (request.ActionType)
        {
            case ActionType.Foo:
                var a = new Foo();
                return a.Execute(request);
            case ActionType.Bar:
                var b = new Bar();
                return b.Execute(request);
        }

        return true;
    }
}

public class Foo
{
    public virtual bool Execute(ActionRequest request)
    {
        throw new NotImplementedException();
    }
}

public class Bar
{
    public virtual bool Execute(ActionRequest request)
    {
        throw new NotImplementedException();
    }
}

我的NUnit测试如下所示:

[Test]
public void GivenARequestToFooShouldExecuteFoo()
{
    var action = new Mock<Foo>();
    action.Setup(x => x.Execute(It.IsAny<ActionRequest>())).Returns(true);

    var sut = new Mock<Executor>();
    sut.Object.ExecuteAction(new ActionRequest
    {
        ActionType = ActionType.Foo
    });
}

[Test]
public void GivenARequestToBarShouldExecuteBar()
{
    var action = new Mock<Bar>();
    action.Setup(x => x.Execute(It.IsAny<ActionRequest>())).Returns(true);

    var sut = new Mock<Executor>();
    sut.Object.ExecuteAction(new ActionRequest
    {
        ActionType = ActionType.Bar
    });
}

我摆弄了CallBase,但它却无济于事.无论如何,没有这些类的依赖注入和添加接口,我可以轻松解决此问题吗?仅使用Moq就能做到吗?

我目前唯一想做的就是将Execute方法移到Executor类中,并将它们重命名为ExecuteFoo()和ExecuteBar(),但是我要移动很多代码,因此它们必须是局部类(子类)类?).

解决方法:

问题不在于方法的模拟,而在于具体类的创建. Foo和Bar的创建需要从执行器中反转出来.它负责执行动作,而不是创建动作.使用此接口可以处理创建.

public interface IActionCollection : IDictionary<ActionType, Func<IExecute>> {

}

可以认为这是工厂的集合或创造策略的集合.

为操作创建了一个公共界面.

public interface IExecute {
    bool Execute(ActionRequest request);
}

public class Foo : IExecute {
    public virtual bool Execute(ActionRequest request) {
        throw new NotImplementedException();
    }
}

public class Bar : IExecute {
    public virtual bool Execute(ActionRequest request) {
        throw new NotImplementedException();
    }
}

执行器被重构为使用依赖倒置.

public class Executor {
    readonly IActionCollection factories;

    public Executor(IActionCollection factories) {
        this.factories = factories;
    }

    public bool ExecuteAction(ActionRequest request) {
        if (factories.ContainsKey(request.ActionType)) {
            var action = factories[request.ActionType]();
            return action.Execute(request);
        }
        return false;
    }
}

完成重构后,可以使用伪造的动作对执行器进行测试.

public void GivenARequestToFooShouldExecuteFoo() {
    //Arrange
    var expected = true;
    var key = ActionType.Foo;

    var action = new Mock<Foo>();
    action.Setup(x => x.Execute(It.IsAny<ActionRequest>())).Returns(expected);

    var actions = new Mock<IActionCollection>();
    actions.Setup(_ => _[key]).Returns(() => { return () => action.Object; });
    actions.Setup(_ => _.ContainsKey(key)).Returns(true);

    var sut = new Executor(actions.Object);
    var request = new ActionRequest {
        ActionType = ActionType.Foo
    };

    //Act
    var actual = sut.ExecuteAction(request);

    //Assert
    Assert.AreEqual(expected, actual);
}

工厂集合的生产实现如下所示

public class ActionCollection : Dictionary<ActionType, Func<IExecute>>, IActionCollection {
    public ActionCollection()
        : base() {
    }
}

并根据您的具体类型进行相应配置.

var factories = ActionCollection();
factories[ActionType.Foo] = () => new Foo();
factories[ActionType.Bar] = () => new Bar();
上一篇:c#-Moq工作单元


下一篇:CodeGo.net>如何比较复杂的对象与NUnit的单元测试