This is a migrated thread and some comments may be shown as answers.

Error while trying to Mock DataServiceQuery<T>

1 Answer 444 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Javier
Top achievements
Rank 2
Javier asked on 24 Oct 2013, 08:43 AM
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]'..

1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 25 Oct 2013, 01:47 PM
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 StackOverflow.com 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.


Regards,
Kaloyan
Telerik
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.
Tags
General Discussions
Asked by
Javier
Top achievements
Rank 2
Answers by
Kaloyan
Telerik team
Share this question
or