Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.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/2/.net/20.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# Rhino模拟约束和字典参数_C#_.net_Unit Testing_Mocking_Rhino Mocks - Fatal编程技术网

C# Rhino模拟约束和字典参数

C# Rhino模拟约束和字典参数,c#,.net,unit-testing,mocking,rhino-mocks,C#,.net,Unit Testing,Mocking,Rhino Mocks,如何检查接受字典的函数的参数 IDictionary<string, string> someDictionary = new Dictionary<string, string> { {"Key1", "Value1"}, {"Key2", "Value2"} }; Expect.Call(delegate {someService.GetCodes(someDictionary);}).Constraints(...); IDictionary someD

如何检查接受字典的函数的参数

IDictionary<string, string> someDictionary = new Dictionary<string, string> {
  {"Key1", "Value1"},
  {"Key2", "Value2"}
};

Expect.Call(delegate {someService.GetCodes(someDictionary);}).Constraints(...);
IDictionary someDictionary=新字典{
{“Key1”,“Value1”},
{“Key2”,“Value2”}
};
Expect.Call(委托{someService.GetCodes(someDictionary);}).Constraints(…);
基本上,我想验证GetCodes的参数与变量“someDictionary”的值是否相同

我忘了提到正在测试的方法构建字典并将其传递给someService.GetCodes()方法

public void SomeOtherMethod(){
IDictionary dict=新词典{
{“键1”,“值1”},
{“键2”,“值2”}
};
someService.GetCodes(dict);//这将通过!
IDictionary dict2=新词典{
{“键1”,“值1a”},
{“键2a”,“值2”}
};
someService.GetCodes(dict2);//这将失败!
}
因此,我想确保传递给GetCodes方法的字典包含与Expect.Call中指定的字典相同的字典。。。方法

另一个用例是,也许我只是想看看字典的键是否包含“键1”和“键2”,但不关心值。。。或者其他人四处走动。

//安排
// arrange
IDictionary<string, string> someDictionary = new Dictionary<string, string> {
  { "Key1", "Value1" },
  { "Key2", "Value2" }
};
ISomeService someService = MockRepository.GenerateStub<ISomeService>();

// act: someService needs to be the mocked object
// so invoke the desired method somehow
// this is usually done through the real subject under test
someService.GetCodes(someDictionary);

// assert
someService.AssertWasCalled(
    x => x.GetCodes(someDictionary)
);
IDictionary someDictionary=新词典{ {“Key1”,“Value1”}, {“Key2”,“Value2”} }; ISomeService someService=MockRepository.GenerateSub(); //act:someService需要是模拟对象 //因此,以某种方式调用所需的方法 //这通常是通过测试的真实对象来完成的 someService.GetCodes(someDictionary); //断言 调用了someService.assertwas( x=>x.GetCodes(someDictionary) );

更新:

以下是如何断言参数值:

someService.AssertWasCalled(
    x => x.GetCodes(
        Arg<IDictionary<string, string>>.Matches(
            dictionary => 
                dictionary["Key1"] == "Value1" && 
                dictionary["Key2"] == "Value2"
        )
    )    
);
someService.AssertWasCalled(
x=>x.GetCodes(
Arg.匹配(
字典=>
字典[“Key1”]=“Value1”&&
字典[“键2”]=“值2”
)
)    
);

更新2:

正如@mquander在注释中所建议的,可以使用LINQ缩短前面的断言:

someService.AssertWasCalled(
    x => x.GetCodes(
        Arg<IDictionary<string, string>>.Matches(
            dictionary => 
                dictionary.All(pair => someDictionary[pair.Key] == pair.Value)
        )
    )
);
someService.AssertWasCalled(
x=>x.GetCodes(
Arg.匹配(
字典=>
dictionary.All(pair=>someDictionary[pair.Key]==pair.Value)
)
)
);

我不确定这是我一直在寻找的解决方案。我要检查的是字典中的值,不一定是同一个字典。如果检查它是同一个字典引用,这意味着它具有相同的值,因此不需要将值放入字典中。我更新了我的帖子,以展示一个示例,说明如何验证已使用某些特定参数调用了模拟对象方法。我对Rhino Mock一无所知,但您不能在上面的“更新”中替换谓词体,并将其更改为类似dictionary=>dictionary.All(pair=>someDict[pair.Key]==配对值)?谢谢Darin。这个更新正是我想要的。。谢谢@mquander,是的,确实是字典。所有的都可以用。我添加了一个更新2。
someService.AssertWasCalled(
    x => x.GetCodes(
        Arg<IDictionary<string, string>>.Matches(
            dictionary => 
                dictionary.All(pair => someDictionary[pair.Key] == pair.Value)
        )
    )
);