C# 如何编写用于更新IDictionary的单元测试
我正在尝试为添加或更新C# 如何编写用于更新IDictionary的单元测试,c#,unit-testing,idictionary,C#,Unit Testing,Idictionary,我正在尝试为添加或更新IDictionary 以下是我的添加或更新方法: public static class ExtensionMethods { public static void AddOrUpdate(IDictionary<string, string> Dictionary, string Key, string Val
IDictionary
以下是我的添加或更新方法:
public static class ExtensionMethods
{
public static void AddOrUpdate(IDictionary<string, string> Dictionary, string
Key, string Value)
{
if(Dictionary.Contains(Key)
{
Dictionary[Key] = Value;
}
else
{
Dictionary.Add(Key, Value);
}
}
}
公共静态类扩展方法
{
公共静态void AddOrUpdate(IDictionary Dictionary,string)
键,字符串值)
{
if(Dictionary.Contains)(键)
{
字典[键]=值;
}
其他的
{
添加(键、值);
}
}
}
下面是我的单元测试:
[TestMethod]
public void Checking_Dictionary_Is_Able_To_Update()
{
// Arrange
IDictionary inputDictionary = new Dictionary<string, string>();
inputDictionary.Add("key1", "someValueForKey1");
inputDictionary.Add("key2", "someValueForKey2");
string inputKeyValue = "key1";
string dataToUpdate = "updatedValueForKey1";
// Act
// The following method call produces a conversion error
ExtensionMethods.AddOrUpdate(inputDictionary, inputKeyValue, dataToUpdate);
}
[TestMethod]
公共无效检查\u字典\u能够\u更新()
{
//安排
IDictionary inputDictionary=新字典();
添加(“key1”,“someValueForKey1”);
添加(“key2”,“someValueForKey2”);
字符串inputKeyValue=“key1”;
字符串dataToUpdate=“updatedValueForKey1”;
//表演
//以下方法调用产生转换错误
AddOrUpdate(inputDictionary、inputKeyValue、dataToUpdate);
}
调用AddOrUpdate()时遇到的错误是无法将System.Collections.IDictionary转换为System.Collections.Generic.IDictionary字符串,字符串
任何人都可以指出我可能做错了什么。Arrange正在将测试主题设置为错误的类型
IDictionary
,而被测试的方法需要IDictionary
接下来,被测试的方法调用了错误的方法来检查集合中是否包含键
public static void AddOrUpdate(this IDictionary<string, string> Dictionary, string Key, string Value) {
if (Dictionary.ContainsKey(Key)) { //<-- NOTE ContainsKey
Dictionary[Key] = Value;
} else {
Dictionary.Add(Key, Value);
}
}
publicstaticvoidaddorupdate(此IDictionary字典、字符串键、字符串值){
if(字典容器(键)){//问题在于IDictionary类,测试方法中的IDictionary类是System.Collections.IDictionary类型,而AddOrUpdate方法中的IDictionary类是System.Collections.Generic.IDictionary类型。请检查using语句或使用完全限定类型。本地参数通常大小写较低,以便从其他类型轻松地重新编码问题不是关于单元测试,而是为什么AddOrUpdate()方法抛出。
// Arrange
var inputDictionary = new Dictionary<string, string>();
//...code removed for brevity.
public static void AddOrUpdate(this IDictionary<string, string> Dictionary, string Key, string Value) {
if (Dictionary.ContainsKey(Key)) { //<-- NOTE ContainsKey
Dictionary[Key] = Value;
} else {
Dictionary.Add(Key, Value);
}
}
public static class ExtensionMethods {
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> Dictionary, TKey Key, TValue Value) {
if (Dictionary.ContainsKey(Key)) {
Dictionary[Key] = Value;
} else {
Dictionary.Add(Key, Value);
}
}
}
[TestMethod]
public void _Checking_Dictionary_Is_Able_To_Update() {
// Arrange
var inputDictionary = new Dictionary<string, string>();
inputDictionary.Add("key1", "someValueForKey1");
inputDictionary.Add("key2", "someValueForKey2");
string inputKeyValue = "key1";
string dataToUpdate = "updatedValueForKey1";
// Act
inputDictionary.AddOrUpdate(inputKeyValue, dataToUpdate);
// Assert
//...
}