Error while trying to Mock DataServiceQuery<T>

2 posts, 0 answers
  1. Javier
    Javier avatar
    10 posts
    Member since:
    Apr 2013

    Posted 24 Oct 2013 Link to this post

    Hi I am trying to mock a Property that is of type DataServiceQuery<EntityLock> like this:

    locksViewModel.Query = Mock.Create<DataServiceQuery<EntityLock>>();
     as soon as I try to execute this line I am getting the next exception:

    Initialization method Tgw.Wcs.Windows.U.Test.ComponentTests.Modules.LoadCarriers.LocksViewModelTest.TestInitialize threw exception. System.TypeInitializationException: System.TypeInitializationException: The type initializer for 'System.Data.Services.Client.DataServiceQuery`1' threw an exception. ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

    I have also tried not to Mock this property and initialize it in the constructor of the class that I am trying to test:

    here is part of the constructor:

    public LocksViewModel(IEnumerable<LoadCarrier> selectedLoadCarriers, ILoadCarrierRepository loadCarrierRepository)
              if (selectedLoadCarriers != null && loadCarrierRepository != null)
                  DataContext = loadCarrierRepository.DataModelServiceContext;
                  Query = (DataServiceQuery<EntityLock>)loadCarrierRepository.GetEntityLocks(selectedLoadCarriers.Select(x=>x.Id));

    And here is part of my InitializeTest:
    IQueryable<EntityLock> entityLocks = Mock.Create<IQueryable<EntityLock>>();
               LoadCarrierRepository loadCarrierRepository = Mock.Create<LoadCarrierRepository>();
               loadCarrierRepository.DataContext = Mock.Create<ClientDataModelServiceContext>();
               Mock.Arrange(() => loadCarrierRepository.GetEntityLocks(Arg.IsAny<IEnumerable<Guid>>())).ReturnsCollection(entityLocks);
               Mock.Arrange(() => loadCarrierRepository.GetLockReasons()).ReturnsCollection(new List<string> { "fooLockReason1", "fooLockReason2" });
               locksViewModel = new LocksViewModel(new ObservableCollectionEx<LoadCarrier>() { loadCarrier }, loadCarrierRepository);
    And I am getting this exception: Initialization method Tgw.Wcs.Windows.U.Test.ComponentTests.Modules.LoadCarriers.LocksViewModelTest .TestInitialize threw exception. System.InvalidCastException: System.InvalidCastException: Unable to cast object of type 'Castle.Proxies.IQueryable`1Proxy' to type 'System.Data.Services.Client.DataServiceQuery`1[Tgw.Wcs.Windows.Data.UIModel.EntityLock]'..
  2. Kaloyan
    Kaloyan avatar
    880 posts

    Posted 25 Oct 2013 Link to this post

    Hi Javier,

    Thank you for contacting our support.

    We will require additional information in order to reproduce the issue on our side.

    Can you send us a sample project, reproducing this behavior? It will help us investigate the matter and provide the correct solution for it.

    If sending us a sample project is not possible, please give us the information from the LoaderExceptions property. To acquire it:
    1. Add the failing line to your TestInitialize again:
      locksViewModel.Query = Mock.Create<DataServiceQuery<EntityLock>>();
    2. Check this link in in order to retrieve the LoaderException property;
    3. Debug a test;
    4. Copy the whole information from that property and send it with you next reply.

    Thank you for the help and the understanding in advance.

    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top