Mock DataServiceQuery

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

    Posted 18 Sep 2013 Link to this post


    I am trying to Mock a Data Service Context based on WCF Data Services, here is the code of the DataService

    using System.Data.Services.Client;
    using Tgw.Wcs.Windows.Data.UIModel;
    namespace Tgw.Wcs.Windows.DataServices
        public partial class ClientDataModelServiceContext
            public DataServiceQuery<LoadCarrier> GetLoadCarrier()
                if ((this.GetLoadCarrierSet == null))
                    this.GetLoadCarrierSet = base.CreateQuery<LoadCarrier>("GetLoadCarriers");
                return this.GetLoadCarrierSet;

    What I would like is to Mock this class in order to get a List of LoadCarriers, We can get this list if we execute GetLoadCarrier().Execute() which returns a IEnumerable<LoadCarrier>

    This is the class I am trying to test:

    public class LoadCarrierRepository : ILoadCarrierRepository
            private ClientDataModelServiceContext dataContext;
            public ClientDataModelServiceContext DataContext
                    if (dataContext == null)
                        var uri = WcsEndpointUriUtility.ClientDataQueryServiceAddress;
                        dataContext = new ClientDataModelServiceContext(uri);
                    return dataContext;
            public WcsEndpointUriUtility WcsEndpointUriUtility { get; set; }   
            public IQueryable<LoadCarrier> GetLoadCarriers()
                DataServiceQuery<LoadCarrier> loadCarriers = DataContext.GetLoadCarrier().Expand(x=>x.ContainerType).Expand(x=>x.ContainerType.AttributeView).Expand(x=>x.BookedInventoryAccount).Expand(x=>x.Location).Expand(x=>x.Location.StructureLeaf.WarehouseGeometry).Expand(x => x.EntityLocks).Expand(x=>x.AttributeView);
                return loadCarriers;

    And this is the test:
    /// <summary>
            /// We will test
            /// </summary>
            public void GetLoadCarrierFromDataContextHasData()
                LoadCarrierRepository loadCarrierRepository = new LoadCarrierRepository();
                loadCarrierRepository.WcsEndpointUriUtility = Mock.Create<WcsEndpointUriUtility>();
                Mock.Arrange(() => loadCarrierRepository.DataContext.GetLoadCarrier().Execute()).ReturnsCollection(new List<LoadCarrier>(new List<LoadCarrier>()));
                //Mock.Arrange(() => loadCarrierRepository.GetLoadCarriers()).ReturnsCollection(new List<LoadCarrier>(){new LoadCarrier()});
                var loadCarriers = loadCarrierRepository.GetLoadCarriers();
                Assert.IsTrue(loadCarriers != null);
                Assert.AreEqual(1, loadCarriers.Count());
    I am getting the following exception: Test method Tgw.Wcs.Windows.Test.U.ComponentTests.Modules.LoadCarriers.LoadCarrierRepositoryTest.GetLoadCarrierFromDataContextHasData threw exception: 
    System.ArgumentNullException: Value cannot be null.
    Parameter name: expression
        at System.Linq.Expressions.Expression.RequiresCanRead(Expression expression, String paramName)
       at System.Linq.Expressions.Expression.Convert(Expression expression, Type type, MethodInfo method)
       at System.Linq.Expressions.Expression.Convert(Expression expression, Type type)
       at System.Data.Services.Client.DataServiceQuery`1.Expand(Expression`1 navigationPropertyAccessor)
       at Tgw.Wcs.Windows.Module.LoadCarriers.Repository.LoadCarrierRepository.GetLoadCarriers() in LoadCarrierRepository.cs: line 34
       at Tgw.Wcs.Windows.Test.U.ComponentTests.Modules.LoadCarriers.LoadCarrierRepositoryTest.GetLoadCarrierFromDataContextHasData() in LoadCarrierRepositoryTest.cs: line 39

  2. Kaloyan
    Kaloyan avatar
    880 posts

    Posted 19 Sep 2013 Link to this post

    Hi Javier,

    Thank you for the code provided.

    I assume you will need to change the following arrange:
    Mock.Arrange(() => loadCarrierRepository.DataContext.GetLoadCarrier().Execute()).ReturnsCollection(new List<LoadCarrier>(new List<LoadCarrier>()));
    To something like this:
    Mock.Arrange(() => loadCarrierRepository.DataContext.GetLoadCarrier()).ReturnsCollection(new List<LoadCarrier>(new List<LoadCarrier>()));

    However, if this does not help, I will require a sample project, reproducing the issue in order to assist you further. Please, let me know if this is doable for you.

    I hope this works.

    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