c# - 强制中继到模拟框架

标签 c# moq autofixture

我想创建一个自定义,将 AutoFixture 配置为传递类型,它确实具有关闭到 Moq 的实现。一般情况下我该如何做到这一点?

为了澄清,请考虑:

    public class test
    {
        public string foo;
        public test(IDictionary<string, string> stuffing)
        {
            foo = stuffing["bogus"];
        }
    }

    [TestMethod]
    public void testInjection()
    {
        var fixture = new Fixture();
        bool didThrow;
        try
        {
            var sut = fixture.Create<test>();
            didThrow = false;
        }
        catch
        {
            didThrow = true;
        }
        Assert.IsTrue(didThrow);
    }

测试通过。 AutoFixture 为我提供了一个与 IDictionary 兼容的虚拟集合。我希望测试失败...具体来说,我想将 IDictionary 交给 Moq 并获得默认情况下不会提示缺少键的东西。

我想使用类似 fixture.UseAutoMoq().ForceRelay<Dictionary<string,string>>() 的内容.

我也愿意接受更好的建议...但请考虑密封测试类。我正在测试经常使用此模式的代码,并且我想要围绕它进行测试约定。

最佳答案

要执行您要求的操作,您可以使用 AutoMoqCustomization 添加的 MockRelay 并将其移到 well-known-collections-builders 前面:

[Fact]
public void PassByMovingAutoMoqRelay()
{
    var fixture = new Fixture().Customize(new AutoMoqCustomization());
    var relay = fixture.ResidueCollectors.OfType<MockRelay>().Single();
    fixture.Customizations.Add(relay);

    var sut = fixture.Create<MyClass>(); // Doesn't throw
}

但是,我认为这不是一个好主意,因为大多数 .NET 集合接口(interface)都很庞大 LSP违规行为,并且不能保证自动模拟它们会产生有意义的行为 - 我希望情况相反。

关于c# - 强制中继到模拟框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870254/

相关文章:

c# - MVC5自定义 View 模型,获取更改,更新数据库

c# - 如何(我应该)模拟 DocumentClient 进行 DocumentDb 单元测试?

c# - User.Identity.Name 被模拟后返回 null

c# - 使用 Mock 进行单元测试。父级没有默认构造函数

c# - AutoFixture 可以通过约定注册接口(interface)吗?

c# - MVC 3 中可用的网球场接口(interface) : Errors and Questions

c# - .NET 有免费的 SQL 格式化库吗?

unit-testing - MOQ 和 AutoFixture 之间有什么区别?

moq - Autofixture:使用 AutoMoqCustomization 和 SetupAllProperties 填充模拟接口(interface)的所有属性?

c# - WP工具包 : TiltEffect Doesn't Seem to be Affecting Image