Rhino Mocks Limitations and Dictionary Settings

How would you check the parameters of a function that takes a dictionary?

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

Expect.Call(delegate {someService.GetCodes(someDictionary);}).Constraints(...);

Basically, I want to check that the parameter for GetCodes has the same values ​​as the variable "someDictionary".

I forgot to mention that the test method builds a dictionary and passes it to the someService.GetCodes () method.

public void SomeOtherMethod() {
  IDictionary<string, string> dict = new Dictionary<string, string> {
    {"Key 1", "Value 1"},
    {"Key 2", "Value 2"}
  };

  someService.GetCodes(dict); // This would pass!

  IDictionary<string, string> dict2 = new Dictionary<string, string> {
    {"Key 1", "Value 1a"},
    {"Key 2a", "Value 2"}
  };

  someService.GetCodes(dict2); // This would fail!

}

so I want to make sure that the dictionary passed to the GetCodes method contains the same ones as in the Expect.Call method ....

Another use case might be that maybe I just want to see if the keys of the dictionary contain "Key 1" and "Key 2", but don't care about the values ​​... Or, / p>

+3
1
// 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)
);

UPDATE:

:

someService.AssertWasCalled(
    x => x.GetCodes(
        Arg<IDictionary<string, string>>.Matches(
            dictionary => 
                dictionary["Key1"] == "Value1" && 
                dictionary["Key2"] == "Value2"
        )
    )    
);

UPDATE2:

@mquander , LINQ:

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

Source: https://habr.com/ru/post/1722028/


All Articles