Hi,
I am trying to Mock a Data Service Context based on WCF Data Services, here is the code of the DataService
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:
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