chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
Hi,
I have a test method where I am trying to fake returning an empty collection.

It works when I write

            Isolate.WhenCalled(() => fakeResourceManager.Enumerate<SetResource>(fakeCriteria)).WillReturn(new Collection<SetResource>());


However throws an exception when I write.

List<SetResource> fakeCollection = new List<SetResource>();
            Isolate.WhenCalled(() => fakeResourceManager.Enumerate<SetResource>(fakeCriteria)).WillReturnCollectionValuesOf(fakeCollection);


The exception thrown is -

Cannot swap MsCorLib type: System.Collections.ObjectModel.Collection`1[MyClass.SetResource]

The test method...

    [TestMethod()]
        public void AAACheckProvisioningAdministratorTest1()
        {
            Criteria fakeCriteria = Isolate.Fake.Instance<Criteria>();
            ResourceEnumerateClientManager fakeResourceManager = Isolate.Fake.Instance<ResourceEnumerateClientManager>();
            Isolate.Swap.NextInstance<ResourceEnumerateClientManager>().With(fakeResourceManager);
            Isolate.Swap.NextInstance<Criteria>().With(fakeCriteria);
            
            List<SetResource> fakeCollection = new List<SetResource>();            

            Isolate.WhenCalled(() => fakeResourceManager.Enumerate<SetResource>(fakeCriteria)).WillReturn(new Collection<SetResource>());
            //List<SetResource> fakeCollection = new List<SetResource>();
            //Isolate.WhenCalled(() => fakeResourceManager.Enumerate<SetResource>(fakeCriteria)).WillReturnCollectionValuesOf(fakeCollection);

            SecurityManager target = new SecurityManager();
            PersonResource person = new PersonResource();
            

            bool expected = false; 
            bool actual = target.CheckProvisioningAdministrator(person);

            Assert.AreEqual(expected, actual);
        }   


Thanks.
asked by rakeshna (1.6k points)

3 Answers

0 votes
The behavior you've seen is caused by the way Isolator handles WillReturn/WillReturnCollectionValueOf:

- When using WillReturn an object is returned.
- When using WillReturnCollectionValueOf Isolator return a faked object.

Because of current limitations Isolator cannot fake objects that are defined in MSCorLib - such as List class.

Instead you can return your own collection or array which should work just fine (or using simple WillReturn).
answered by dhelper (11.9k points)
0 votes
Why do all examples state new[] or List<t> then?

I'm having troubles getting it to work as well, and understanding the differences.
answered by dvdstelt (5.3k points)
0 votes
Hi Dennis

If you are talking about Controlling Collections and Indexers chapter in our documentation please notice that we are faking LogCollection which is an example of class that implements IEnumerable the List<RealLogger> will be called whenever IEnumerable members of LogCollection are called.
So actually we are not faking here List<T>. :)
answered by ohad (35.4k points)
...