小编Nic*_*per的帖子

AutoFixture.AutoMoq为一个构造函数参数提供已知值

我刚开始在我的单元测试中使用AutoFixture.AutoMoq,我发现它对于创建我不关心特定值的对象非常有帮助.毕竟,匿名对象创建就是它的全部.

我正在努力的是当我关心一个或多个构造函数参数时.采取ExampleComponent下面:

public class ExampleComponent
{
    public ExampleComponent(IService service, string someValue)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

我想写一个测试,我提供了一个特定的值,someValue但是IServiceAutoFixture.AutoMoq自动创建.

我知道如何使用FreezeIFixture来保持一个已注入组件的已知值,但我不太明白如何提供我自己的已知值.

这是我理想的做法:

[TestMethod]
public void Create_ExampleComponent_With_Known_SomeValue()
{
    // create a fixture that supports automocking
    IFixture fixture = new Fixture().Customize(new AutoMoqCustomization());

    // supply a known value for someValue (this method doesn't exist)
    string knownValue = fixture.Freeze<string>("My known value");

    // create an ExampleComponent with my known value injected 
    // but without …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing autofixture automocking

22
推荐指数
5
解决办法
8749
查看次数

自动锁定SUT

我已经阅读了Mark Seeman 关于自动模拟文章,我现在正在编写一个基于该文章的可重复使用的windsor容器.

我执行Mark的文章(基本上直接复制)

主要工作是在AutoMoqResolver课堂上完成的.只要类依赖于接口,这将提供模拟:

public class AutoMoqResolver : ISubDependencyResolver
{
    private readonly IKernel kernel;

    public AutoMoqResolver(IKernel kernel)
    {
        this.kernel = kernel;
    }

    public bool CanResolve(
        CreationContext context,
        ISubDependencyResolver contextHandlerResolver,
        ComponentModel model,
        DependencyModel dependency)
    {
        return dependency.TargetType.IsInterface;
    }

    public object Resolve(
        CreationContext context,
        ISubDependencyResolver contextHandlerResolver,
        ComponentModel model,
        DependencyModel dependency)
    {
        var mockType = typeof(Mock<>).MakeGenericType(dependency.TargetType);
        return ((Mock)this.kernel.Resolve(mockType)).Object;
    }
}
Run Code Online (Sandbox Code Playgroud)

AutoMoqResolver被添加到使用以下实施的容器IWindsorInstaller接口:

public class AutoMockInstaller<T> : IWindsorInstaller
{
    public void Install(
        IWindsorContainer container,
        IConfigurationStore …
Run Code Online (Sandbox Code Playgroud)

c# unit-testing castle-windsor autofixture automocking

3
推荐指数
1
解决办法
1227
查看次数