我有一个公开接口(interface)的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)失败,并显示错误消息



这似乎变得非常复杂。有没有一种方法可以让Visual Studio为Account&Contract生成接口(interface),或者让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; }
}

最佳答案

对于那些通过搜索引擎发现此问题的人。

要消除手动创建DataServiceCollection时出现的“DataServiceContext ...无法确定”异常,请使用TracktMode参数设置为None的overloaded constructor


new DataServiceCollection<Contract>(blah, TrackingMode.None)

关于.net - 模拟WCF客户端的IQueryable成员,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4149982/

10-17 02:14