Moq does It.Is<List<List<string>>>(x => x.Count == 10) do what I think it should do?
I am passing this into the constructor of an object I am unit testing
It.Is<List<List<s开发者_JAVA技巧tring>>>(x => x.Count == 10)
but when I step into the constructor, this statement resolves to null instead of a a List<List<string>>
with a Count of 10. Am I misunderstanding how this works?
The It.Is
method is not meant to be called. Actually, I think it should just throw, instead of returning the default value of the type.
It is meant to be used in the expression trees used to setting expectations:
interface IFoo { bool DoSomething(IList<IList<string>> strings); }
var mock = new Mock<IFoo>();
mock.Setup(f => f.DoSomething(It.Is<IList<IList<string>>>(l => l.Count == 10))
.Returns(true);
The example sets up a mock object of IFoo
that will return true when passed an IList<IList<string>>
object with 10 elements. This means that after the following call, result
will be true:
IList<IList<string>> listWith10Elements = // create a list with 10 elements
bool result = mock.Object.DoSomething(listWith10Elements);
If you're passing something into the constructor of an object, you would normally use the proxy object from a mock, with Setup
on that mock providing any context which your class needs.
For instance:
var mock = new Mock<List<List<string>>>();
mock.Setup(x => x.Count()).Returns(10);
var myClass = new MyClass(mock.Object);
Use Verify
to check interactions. What you have there is a matcher, which you can use in Setup
and Verify
to match particular types of arguments.
Except, of course, that you won't be able to mock List
because it doesn't have the virtual methods you're after. Try using, and mocking, ICollection<List<string>>
instead.
I think this should do what you want. Hope this helps.
精彩评论