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
WillReturnCollectionValuesOf cannot accept mock values?

Here's the production code
    public class ListWrapper
    {
        public List<SimpleClass> lists
        {
            get; set;
        }
    }
    public abstract class SimpleClass
    {
        public string ID
        {
            get;
            set;
        }

        public string Node
        {

            get
            {
                throw new NotImplementedException();
            }
        }
    }


Here's the test code
        [Test, Isolated]
        public  void TestListWrapper()
        {
            ListWrapper lists = Isolate.Fake.Instance<ListWrapper>(Members.ReturnRecursiveFakes);
            Isolate.WhenCalled(()=>lists.lists).WillReturnCollectionValuesOf(new List<SimpleClass>()
                                                                                 {
                                                                                     Isolate.Fake.Instance<SimpleClass>()
                                                                                 });
        }


And the exception blows up at the WillReturnCOllectionValuesOf line, because of this exception:
failed: TypeMock.TypeMockException : 
*** Cannot mock types from mscorlib assembly.
   at TypeMock.MockManager.a(Type A_0, String A_1, Boolean A_2)
   at TypeMock.MockManager.a(Type A_0, String A_1)
   at TypeMock.MockManager.f(Type A_0)
   at TypeMock.MockManager.Mock(Type type, Constructor mockConstructors)
   at TypeMock.MockManager.MockObject(Type type, Constructor mockConstructors, Object[] args)
   at TypeMock.MockManager.MockObject[TMockedType]()
   at TypeMock.ArrangeActAssert.Recorder`1.a(IEnumerable A_0)


I think this is a bug, because the list should be mockable, there is nothing from mscorlib I afraid.
________
box vaporizer
asked by nsoonhui (59.1k points)

7 Answers

0 votes
Hi Soon Hui,

This issue happens because you are trying to perform the collection values swapping on a List<T>, a type defined in MSCorLib. This is not supported as a known limitation of Isolator. If the property would return an IList<SimpleClass> instead, this could should work.

Thanks,
Doron,
Typemock support
answered by doron (17.2k points)
0 votes
Thanks, however as far as I can see, both List and IList are both contained inside the MSCorLib. Now the question is why one can be mocked but another can't?
________
Chrysler FirePower engine history
answered by nsoonhui (59.1k points)
0 votes
Good question - the limitation regards to faking concrete objects from MSCorLib. You can still fake interfaces from MSCorLib safely.
Doron
answered by doron (17.2k points)
0 votes
I see, that would be a sufficient explanation were i not see the example from your blog.

In that example list is used for mocking purpose. Maybe there is a catch somewhere I miss..
________
MyKey
answered by nsoonhui (59.1k points)
0 votes
I guess the example was not clear enough; the class under test here is under the SharePoint object model which implements list interfaces outside of MSCorLib; specifically we are swapping values for collections implemeneted in SPListCollection and SPListItemCollection, both in Microsoft.Sharepoint.dll.


Note that you can click the following link to learn more on using sharepoint!
answered by doron (17.2k points)
0 votes
HI,

I afraid that the List that I used and the list the post used are the same, as can be shown from the below method
[TestMethod]
public void SwappingSharepointCollections_IterateOverCollectionWithForEach_SwappedIteratorsAreUsed()
{
  SPSite fakeSite = Isolate.Fake.Instance<SPSite>(Members.ReturnRecursiveFakes);
  Isolate.Swap.NextInstance<SPSite>().With(fakeSite);

 [i] Isolate.WhenCalled(() => fakeSite.OpenWeb().Lists[2].Items).WillReturnCollectionValuesOf(new List<SPItem>
                                  {
                                      Isolate.Fake.Instance<SPItem>(),
                                      Isolate.Fake.Instance<SPItem>(),
                                      Isolate.Fake.Instance<SPItem>()
                                  });[/i]
  SharepointUsingClass target = new SharepointUsingClass();
  target.DeepIterationWithForEach();

  var fakeItemList = fakeSite.OpenWeb().Lists[2].Items;
  foreach (SPItem item in fakeItemList)
  {
      Isolate.Verify.WasCalledWithAnyArguments(() => item.Update());
  }
}


As one can see, when WillReturnCollectionValuesOf is applied, it is applied on List<SPItem>, not SPListCollection or SPListItemCollection.
________
vapir air one vaporizer
answered by nsoonhui (59.1k points)
0 votes
Change the statement to WillReturn (you only need the CollectionValues when the collection types are not the same)
[Isolated,Test]
public  void TestListWrapper()
{
  ListWrapper lists = Isolate.Fake.Instance<ListWrapper>(Members.ReturnRecursiveFakes);
  Isolate.WhenCalled(()=>lists.lists).
     WillReturn(new List<SimpleClass>()
                     {
                        Isolate.Fake.Instance<SimpleClass>()
                     });
}
answered by eli (5.7k points)
...