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

Mock DataServiceQuery

1 Answer 45 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Javier
Top achievements
Rank 2
Javier asked on 18 Sep 2013, 04:11 PM
Hi,

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
        {
            get
            {
                if (dataContext == null)
                {
                    var uri = WcsEndpointUriUtility.ClientDataQueryServiceAddress;
                    dataContext = new ClientDataModelServiceContext(uri);
                }
                return dataContext;
            }
        }
 
        [Dependency]
        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>
        [TestMethod]
        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




1 Answer, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 19 Sep 2013, 11:12 AM
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.

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