ShouldLookLike for a List

Solved!
Posted in General by TranVoNB Thu Oct 22 2015 10:32:25 GMT+0000 (UTC)·3·Viewed 378 times

How can I assert a list, something like below: myResult.ShouldLookLike(() => new List<MyObject>(){ new MyObject(){ //resultId is Guid, so this should be ignore ObjectName = "result name 1", ObjectValue = "object value 1" }, new MyObject(){ //resultId is Guid, so this should be ignore ObjectName = "result name 2", ObjectValue = "object value 2" } }
Matt Honeycutt
Oct 22, 2015

I believe you should be able to pass an array in for your "expected" object and get the behavior you're after:

myResult.ShouldLookLike(() =>
new [] {
new MyObject(){
//You can even make sure resultId is populated
ResultId = Any.NonDefaultValueOf<Guid>(),
ObjectName = "result name 1",
ObjectValue = "object value 1"
},
new MyObject(){
//You can even make sure resultId is populated
ResultId = Any.NonDefaultValueOf<Guid>(),
ObjectName = "result name 2",
ObjectValue = "object value 2"
}
}

TranVoNB
Oct 23, 2015

Sorry Matt, it doesn't work.
This error show up when compile : "Cannot convert lambda expression to type 'System.Collections.Generic.List<MyObject>' because it is not a delegate type"

I checked source code on GitHub:

"var newArrayExpression = matchFunc.Body as NewArrayExpression;"

ShouldLookLike method convert body of Expression to NewArrayExpression but myResult is a list, it doesn't match generic type declaration of method.
So I modified code to:

myResult
.ToArray()/This convert list to array?
.ShouldLookLike(() =>
new [] {
new MyObject(){
//You can even make sure resultId is populated
ResultId = Any.NonDefaultValueOf<Guid>(),
ObjectName = "result name 1",
ObjectValue = "object value 1"
},
new MyObject(){
//You can even make sure resultId is populated
ResultId = Any.NonDefaultValueOf<Guid>(),
ObjectName = "result name 2",
ObjectValue = "object value 2"
}
})

This works :)


Matt Honeycutt marked this as solved
Markdown is allowed