Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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# 与私有集合相关的单元测试公共方法_C#_Unit Testing_Nunit - Fatal编程技术网

C# 与私有集合相关的单元测试公共方法

C# 与私有集合相关的单元测试公共方法,c#,unit-testing,nunit,C#,Unit Testing,Nunit,我的类有一个私有的lazy字典和一些像这样与之交互的方法 public class MyClass : IExampleClass { private Lazy<Dictionary<string, IMyObject>> myObjects = new Lazy<Dictionary<string, IMyObject>>(); private Dictionary<string, IMyObject> MyObjec

我的类有一个私有的lazy字典和一些像这样与之交互的方法

public class MyClass : IExampleClass
{
    private Lazy<Dictionary<string, IMyObject>> myObjects = new Lazy<Dictionary<string, IMyObject>>();
    private Dictionary<string, IMyObject> MyObjects
    {
        get
        {
            return myObjects.Value;
        }
    }

    public class MyClass()
    {}

    public void RegisterObject(string name, IMyObject object)
    {
        logger.LogInfo("Executed RegisterObject etc.");

        if (String.IsNullOrEmpty(name) == false && object != null && MyObjects.ContainsKey(name) == false)
        {
             MyObjects.Add(name, object);
        }
        else
        {
             logger.LogWarning("Cannot register the object because etc.");
        }
    }

    public IMyObject GetObject(string name)
    {
        logger.LogInfo("Executed GetObject etc.");

        IMyObject obj = null;
        if (String.IsNullOrEmpty(name) == false)
        {
            Objects.TryGetValue(name, out obj);
        }

        return obj;
    }
}
除此之外,当我试图插入字典中已有的元素时,我如何处理测试

    [Test]
    public void RegisterObject_AlreadyInserted_DoNothing()
    {
        //Arrange
        string inputName = "test";
        IMyObject inputObject = A.Dummy<IMyObject>();
        IExampleClass sut = new MyClass();

        // ??? 
        sut.RegisterObject(inputName, inputObject);
        // how can i effectively test the method if i'm using it more than once???

        //Act
        sut.RegisterObject(inputName, inputObject);

        //Assert
        IMyObject actualValue = sut.GetObject(inputName);

        // ???
        Assert.AreEqual(inputObject, actualValue);
        // What i've tested here? The fact that the dictionary contains
        // the object doesn't mean that the second call of RegisterObject
        // is ignored.
    }
所以最后我要问:

测试RegisterObject在其内部调用GetObject是否正确? 如果我必须测试第二次插入是否被忽略,如何安排词典? 另一方面,我对RegisterObject有7个单独的测试:

带有NullName和NullObject的RegisterObject_不包含任何内容 注册表对象为NullName,有效对象为Nothing 不带EmptyName和NullObject的注册表对象 注册对象不带空名称和有效对象 注册表对象不带有效名称和空对象 带有有效名称和有效对象的注册表对象 注册表对象已插入任何内容 太过分了?对于这种简单的行为,测试太多了

测试RegisterObject在其内部调用GetObject是否正确

考虑到我们对MyClass的公共接口所知的只是它有RegisterObject和GetObject,我认为您无法做任何其他事情。 即使你能做点别的,我也不会。您希望确保该类以特定方式为其使用者运行,因此请像使用者一样使用它。这就是你如何判断它是否在做它的工作

如果我必须测试第二次插入是否被忽略,如何安排词典


从消费者的角度考虑这一点。你想发生什么?似乎您希望显示,如果在现有键下插入第二个对象,则该方法将以静默方式成功,但存储的值是原始值。我们怎么知道储存了什么?通过调用GetObject。所以我认为你做的测试很好,但我会使用不同的值来插入。这样你就可以知道是否有什么改变了。如果GetObject返回原始值,则您的方法是正确的。

谢谢,我没有想过在insert上使用其他对象来验证任何更改。我对在RegisterObject的测试中使用GetObject并不完全满意,但找不到任何其他方法。
    [Test]
    public void RegisterObject_AlreadyInserted_DoNothing()
    {
        //Arrange
        string inputName = "test";
        IMyObject inputObject = A.Dummy<IMyObject>();
        IExampleClass sut = new MyClass();

        // ??? 
        sut.RegisterObject(inputName, inputObject);
        // how can i effectively test the method if i'm using it more than once???

        //Act
        sut.RegisterObject(inputName, inputObject);

        //Assert
        IMyObject actualValue = sut.GetObject(inputName);

        // ???
        Assert.AreEqual(inputObject, actualValue);
        // What i've tested here? The fact that the dictionary contains
        // the object doesn't mean that the second call of RegisterObject
        // is ignored.
    }