Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用NSubstitute(或其他内容)进行统一和自动模拟_C#_Unit Testing_Unity Container_Nsubstitute_Automocking - Fatal编程技术网

C# 使用NSubstitute(或其他内容)进行统一和自动模拟

C# 使用NSubstitute(或其他内容)进行统一和自动模拟,c#,unit-testing,unity-container,nsubstitute,automocking,C#,Unit Testing,Unity Container,Nsubstitute,Automocking,我的问题源于这个问题: 以下是答案中的课程: protected override void Initialize() { var strategy = new AutoMockingBuilderStrategy(Container); Context.Strategies.Add(strategy, UnityBuildStage.PreCreation); } class AutoMockingBuilderStrategy : BuilderStrategy {

我的问题源于这个问题:

以下是答案中的课程:

protected override void Initialize()
{
    var strategy = new AutoMockingBuilderStrategy(Container);

    Context.Strategies.Add(strategy, UnityBuildStage.PreCreation);
}

class AutoMockingBuilderStrategy : BuilderStrategy
{
    private readonly IUnityContainer container;
    private readonly Dictionary<Type, object> substitutes 
       = new Dictionary<Type, object>();

    public AutoMockingBuilderStrategy(IUnityContainer container)
    {
        this.container = container;
    }

    public override void PreBuildUp(IBuilderContext context)
    {
        var key = context.OriginalBuildKey;

        if (key.Type.IsInterface && !container.IsRegistered(key.Type))
        {
            context.Existing = GetOrCreateSubstitute(key.Type);
            context.BuildComplete = true;
        }
    }

    private object GetOrCreateSubstitute(Type type)
    {
        if (substitutes.ContainsKey(type))
            return substitutes[type];

        var substitute = Substitute.For(new[] {type}, null);

        substitutes.Add(type, substitute);

        return substitute;
    }
}
问题是,如果我创建两个这样的对象,模拟将指向同一个对象

如果我删除方法GetOrCreateSubstitute()中的CONTAINS检查,那么每次。。。但是,我如何访问特定对象的mock来设置对它的期望呢-(


我希望我能清楚地回答这个问题!!

我们进行了一次内部团队讨论,我更好地理解了模拟应该是单体的。通过设计,我们不希望每个创建的对象都有不同的模拟对象

模拟是针对类的,而不是针对方法的。因此,下面的伪代码将是实现我所做工作的完美方式

var obj1 = Resolve<IMyInterface>();
var obj2 = Resolve<IMyInterface>();
var innerDependency = Resolve<IInnerType>(); // Returns the same object that is shared by above 2 objs

innerDependency.SetExpection(Some expectation);
obj1.PerformAction();
innerDependency.Assert();

innerDependency.SetExpection(Some *different* expectation);
obj2.PerformAction();
innerDependency.Assert();
var obj1=Resolve();
var obj2=Resolve();
var innerDependency=Resolve();//返回上述两个对象共享的同一个对象
SetExpection(一些期望);
obj1.PerformAction();
断言();
SetExpection(一些*不同的*期望);
obj2.PerformAction();
断言();
因此,这个问题的借口是错误的。我们没有这样的灵活性,因为我们不想这样做

- ActualObject
    - InnerActualObjectDependency
        - MockedDependency (since this was not mapped in Unity, a mock was created)
var obj1 = Resolve<IMyInterface>();
var obj2 = Resolve<IMyInterface>();
var innerDependency = Resolve<IInnerType>(); // Returns the same object that is shared by above 2 objs

innerDependency.SetExpection(Some expectation);
obj1.PerformAction();
innerDependency.Assert();

innerDependency.SetExpection(Some *different* expectation);
obj2.PerformAction();
innerDependency.Assert();