Hi all,
I am working on a SharePoint 2016 project where I would like to fake all instances of type SPFieldUserValueCollection. SPFieldUserValueCollection itself inherits from List<SPFieldUserValue> and thus allows access to Linq extension methods like .Where() and .Select().
Mocking .Where() and .Select() quickly turns into an unreadable mess, thus I looked around in the docs and found .WillReturnCollectionValuesOf(). Considering the documentation:
Isolate. WhenCalled(() => <your_collection>).WillReturnCollectionValuesOf(<items>);
I thought I'd give it a try:
var fakeFieldUserValue = Isolate.Fake.Instance<SPFieldUserValue>();
var fakeFieldUserValueCollection = Isolate.Fake.Instance<SPFieldUserValueCollection>();
Isolate.WhenCalled(() => fakeFieldUserValueCollection)
.WillReturnCollectionValuesOf(
new List<SPFieldUserValue> { fakeFieldUserValue }
);
In this example, <your_collection> is equal to fakeFieldUserValueCollection. However, the code above throws an ArgumentOutOfRangeException:
Failure:
System.ArgumentOutOfRangeException : Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Stack Trace:
at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at _oJiwc1IhSkoCbhfIhmasuFjMTjP._02sePMelqT6pMKfF7IPNjRlbfBH`1._pVOOtbtDYsEhhHWcFc9z9Nx6Oqp._rdAcAO27axp3r80m0weVkSBMc8k()
at TypeMock.ArrangeActAssert.ExpectationEngine._74WqWsXrKtdsupvOupEdcsRuDlD(Object , Boolean , Func`1 , Action , Action , Action , Boolean )
at _oJiwc1IhSkoCbhfIhmasuFjMTjP._02sePMelqT6pMKfF7IPNjRlbfBH`1._Ovbb5hQAjT1KsRIMeW6DKXfq09E(IEnumerable )
at UnitTests.Model.DemoTests.TheFaultyTest()
Best wishes
mzietlow