代码之家  ›  专栏  ›  技术社区  ›  FOR

模拟DataServiceQuery<TElement>

  •  5
  • FOR  · 技术社区  · 17 年前

    想象一下ASP。NET MVC应用程序,其中控制器与ADO。NET数据服务,它封装了我们模型的存储(例如,为了方便起见,我们将阅读一份客户列表)。通过引用该服务,我们得到一个从DataServiceContext继承的生成类:

    namespace Sample.Services
    {
      public partial class MyDataContext : global::System.Data.Services.Client.DataServiceContext
      {
        public MyDataContext(global::System.Uri serviceRoot) : base(serviceRoot) { /* ... */ }
    
        public global::System.Data.Services.Client.DataServiceQuery<Customer> Customers
        {
          get
          {
            if((this._Customers==null))
            {
              this._Customers = base.CreateQuery<Customer>("Customers");
            }
            return this._Customers;
          }
        }
        /* and many more members */
      }
    }
    

    namespace Sample.Controllers
    {
      public class CustomerController : Controller
      {
        private IMyDataContext context;
    
        public CustomerController(IMyDataContext context)
        {
          this.context=context;
        }
    
        public ActionResult Index() { return View(context.Customers); }
      }
    }
    

    [TestFixture]
    public class TestCustomerController
    {
      [Test]
      public void Test_Index()
      {
        MockContext mockContext = new MockContext();
        CustomerController controller = new CustomerController(mockContext);
    
        var customersToReturn = new List<Customer>
        {
          new Customer{ Id=1, Name="Fred" },
          new Customer{ Id=2, Name="Wilma" }
        };
        mockContext.CustomersToReturn = customersToReturn;
    
        var result = controller.Index() as ViewResult;
    
        var models = result.ViewData.Model;
    
        //Now we have to compare the Customers in models with those in customersToReturn,
        //Maybe by loopping over them?
        foreach(Customer c in models) //*** LINE A ***
        {
          //TODO: compare with the Customer in the same position from customersToreturn
        }
      }
    }
    

    namespace Sample.Services
    {
      public interface IMyDataContext
      {
        DataServiceQuery<Customer> Customers { get; }
        /* and more */
      }
    }
    

    public class MockContext : IMyDataContext
    {
      public IList<Customer> CustomersToReturn { set; private get; }
    
      public DataServiceQuery<Customer> Customers { get { /* ??? */ } }
    }
    

    在Customers getter中,我们想实例化一个DataServiceQuery实例,用CustomersToReaturn中的Customers填充它,并返回它。我遇到的问题是:

    MSDN

    2.

    namespace Sample.Tests.Controllers.Mocks
    {
      public class MockContext : DataServiceContext, IMyDataContext
      {
        public MockContext() :base(new Uri("http://www.contoso.com")) { }
    
        public IList<Customer> CustomersToReturn { set; private get; }
    
        public DataServiceQuery<Customer> Customers
        {
          get
          {
            var query = CreateQuery<Customer>("Customers");
            query.Concat(CustomersToReturn.AsEnumerable<Customer>());
            return query;
          }
        }
      }
    }
    

    http://www.contoso.com 不提供我们的服务。即使LINE A试图获取模型中的元素数量,也会触发相同的错误。 提前感谢。

    2 回复  |  直到 17 年前
        1
  •  0
  •   Dror Helper    17 年前

    IDataServiceQuery 有两种实现方式:

    • DataServiceQueryWrapper
    • MockDataServiceQuery

    然后我使用 ViewModel服务查询 无论我以前在哪里使用 DataServiceQuery .

    public interface IDataServiceQuery<TElement> : IQueryable<TElement>, IEnumerable<TElement>, IQueryable, IEnumerable
    {
        IDataServiceQuery<TElement> Expand(string path);
    
        IDataServiceQuery<TElement> IncludeTotalCount();
    
        IDataServiceQuery<TElement> AddQueryOption(string name, object value);
    }
    

    需要a IQueryable

    ViewModel服务查询 方法,我目前刚刚返回 this

    // (in DataServiceQueryWrapper.cs)
    public IDataServiceQuery<TElement> Expand(string path)
    {
        return new DataServiceQueryWrapper<TElement>(_query.Expand(path));
    }
    

    // (in MockDataServiceQuery.cs)
    public IDataServiceQuery<TElement> Expand(string path)
    {
        return this;
    }
    
        2
  •  4
  •   dcstraw    16 年前

    var fake = Isolate.Fake.Instance<DataServiceQuery>();
    

    推荐文章