So assume I am mocking the following class:
public class ClassAParams
{
public int RequestedId { get; set; }
public string SomeValue { get; set; }
}
public class ClassA
{
public void ExecuteAction(ClassAPa开发者_如何学Crams executeParams) {}
}
Now say I have another class (let's call it ClassB
) that I am creating a unit testing for, and I want to make sure that when ClassB.Execute()
is called, that ClassB
calls ClassA.ExecuteAction()
but I want to make sure that the parameter it calls that method with has a ClassAParams.RequestedId
value of 1.
Normally, I would handle this by doing myMock.Verify(x => x.ExecuteAction(new ClassAParams { RequestedId = 1, SomeValue = "something" }));
The problem is, I do not want to check the value of the SomeValue
parameter, or any other ClassAParams
properties in this unit test. The other properties will be checked in other unit tests, but having to verify that it's called with the correct properties in every unit test (even when I don't care in the scope of a specific unit tests) will make unit maintenance annoying.
Is there any way to use Moq to verify that the structure that gets passed into a mocked method only has certain properties as a specific value, and ignore the rest?
An overload exists for mock.Verify
which allows you to test something. Here is an example that should work for your test.
classA.Verify(
a => a.ExecuteAction(
It.Is<ClassAParams>(p => p.RequestedId == 12)
)
);
Which means "For any ClassAParams
passed as argument, verify RequestId
equals 12".
精彩评论