I want to unit test that a method under test calls a stubbed object and method with the right parameters. The problem is that one of the parameters is a dynamic (ExpandoObject). If "data" (variable below) was a typed object it works as expected.

    ...
    [Test]
    public void MethodTest_WhenSomething_ExpectResult()
    {
      ...
      dynamic data = new ExpandoObject();
      data.Id = param1;
      data.Name = param2;
      var myClass= MockRepository.GenerateStub<IMyClass>();
      myClass.Stub(x => x.MyMethod("hello", data).Returns(expectedResult);
      ...
      var actualResult = anotherClass.MethodUnderTest(param1, param2);

      Assert.IsNotNull(actualResult);
    }

Any ideas how I can do this? BTW, I dont want to "IgnoreArguments" I am testing that right params are being passed in.

TIA

有帮助吗?

解决方案

I assume you need to define the stub which returns expectedResult when the second parameter has the right values in the fields Id and Name.

But now you stub is defined to return expectedResult when the second parameter is the same as the data object.

If so, then you just need to modify the Stub definition:

myClass
    .Stub(x => x.MyMethod(
            Arg<string>.Is.Equal("hello"),
            Arg<IDictionary<string, object>>.Matches(d => d["Id"].Equals(param1) && d["Name"].Equals(param2))
        ))
    .Return(expectedResult);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top