我有一个公开接口的WCF数据服务:
public interface IAccountService
{
IQueryable<Account> Accounts { get; }
IQueryable<Contract> Contracts { get; }
}
[DataServiceKey("ID")]
public class Account
{
public virtual Guid ID { get; set; }
public virtual string AccountName { get; set; }
public virtual string Address1 { get; set; }
public virtual string Address2 { get; set; }
public virtual string City { get; set; }
public virtual string State { get; set; }
public virtual string Zip { get; set; }
public virtual string Phone { get; set; }
public virtual string Fax { get; set; }
public virtual string Status { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual IEnumerable<Contract> Contracts { get; set; }
}
[DataServiceKey("ID")]
public class Contract
{
public virtual Guid ID { get; set; }
public virtual string ContractCode { get; set; }
public virtual string ContractName { get; set; }
public virtual DateTime StartDate { get; set; }
public virtual int UnitQty { get; set; }
public virtual double UnitPrice { get; set; }
}
我想模拟服务客户端进行单元测试。我想写一个像以下的测试:
[Test]
public void MockContractsFromAccount()
{
var acctSvc = new Mock<IAccountService>();
var blah = new Contract[] { new Contract { ContractCode = "BLAH001", ContractName = "blah 1" },
new Contract { ContractCode = "BLAH002", ContractName = "blah 2" } };
var blee = new Contract[] { new Contract { ContractCode = "BLEE001", ContractName = "blee 1" } };
var accts = new Account[] { new Account { AccountName = "blah", State = "WY", Contracts = new DataServiceCollection<Contract>(blah) },
new Account { AccountName = "blee", State = "CO", Contracts = new DataServiceCollection<Contract>(blee) } };
acctSvc.SetupGet(x => x.Accounts).Returns(accts.AsQueryable());
var result = from x in acctSvc.Object.Accounts
where x.State == "CO"
select x;
Assert.That(result, Has.Count.EqualTo(1));
Assert.That(result.First().Contracts, Has.Count.EqualTo(2));
Assert.That(result, Is.All.EquivalentTo(blah));
}
但代码在new DataServiceCollection<Contract>(blah)
失败并显示错误消息
SomeTests.MockContractsFromAccount: System.ArgumentException:无法确定DataServiceCollection实例所属的DataServiceContext。 DataServiceContext必须在DataServiceCollection构造函数中提供,或者用于创建DataServiceCollection中项目的源DataServiceQuery或QueryOperationResponse对象
这似乎变得非常复杂。有没有办法让Visual Studio为Account&amp; amp;合同或让accts.Contracts
返回IQueryable<Contract>
而不是DataServiceCollection<Contract>
?也许是其他一些简单的解决方案来模拟这项服务?
为了清楚起见,svcUtil.exe正在生成一个如下所示的Account类:
public class Account
{
public Guid ID { get; set; }
public string AccountName { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string Status { get; set; }
public DateTime StartDate { get; set; }
// It descends from IEnumerable<Contract>
public DataServiceCollection<Contract> Contracts { get; set; }
}
答案 0 :(得分:7)
对于那些通过搜索引擎发现此问题的人。
要在手动创建DataServiceCollection时消除“DataServiceContext ...无法确定”异常,请使用overloaded constructor并将TrackingMode参数设置为None。
即,
new DataServiceCollection<Contract>(blah, TrackingMode.None)
答案 1 :(得分:-1)
由于Contracts
属性的类型为IEnumerable
,因此您可以创建任何可枚举的集合。
所以,而不是
Contracts = new DataServiceCollection<Contract>(blah)
只需使用
Contracts = new [] { blah }
这将确保您的伪造物品将断开连接。