无法模拟 TableDomainService 之类的内容,其中 EntityContext 在类定义中设置
我正在尝试专门使用 Moq 来学习和实现 TDD,但我遇到了一个我不知道如何模拟的设计:
namespace RIACompletelyRelativeWebService.Web.Services
{
[EnableClientAccess]
public class AncestorDomainService : TableDomainService<AncestorEntityContext>
{
public AncestorDomainService()
{
//this.EntityContext = new AncestorEntityContext();
}
public IQueryable<AncestorEntity> GetAncestorEntities()
{
return this.EntityContext.AncestorEntities;
}
public void AddAncestorEntity(AncestorEntity entity)
{
this.EntityContext.AncestorEntities.Add(entity);
}
}
}
我认为我需要模拟 TableDomainService,以便我可以在不启动 Azure 的情况下测试我的 AncestorDomainService 逻辑。我厌倦了这样的事情:
public class AncestorDomainService<TEntityContext> : TableDomainService<TEntityContext> where TEntityContext is a TableEntityContext
但是,TableDomainService 不喜欢使用泛型。我还尝试设置 EntityContext 但它是只读的。我见过其他人使用通用的 DomainService 和 Repository 设计模式,但由于 TableDomainService 允许我在幕后使用 Azure 表,所以我认为我必须坚持使用 TableDomainService<>。我是否只需要伪造 TableDomainService、TableEntityContext 和返回的 TableEntitySet 即可?
I am trying to learn and implement TDD specifically using Moq and I have come up against a design that I can't figure out how to mock:
namespace RIACompletelyRelativeWebService.Web.Services
{
[EnableClientAccess]
public class AncestorDomainService : TableDomainService<AncestorEntityContext>
{
public AncestorDomainService()
{
//this.EntityContext = new AncestorEntityContext();
}
public IQueryable<AncestorEntity> GetAncestorEntities()
{
return this.EntityContext.AncestorEntities;
}
public void AddAncestorEntity(AncestorEntity entity)
{
this.EntityContext.AncestorEntities.Add(entity);
}
}
}
I think I need to mock the TableDomainService so that I can test my AncestorDomainService logic without firing up Azure. I tired something like this:
public class AncestorDomainService<TEntityContext> : TableDomainService<TEntityContext> where TEntityContext is a TableEntityContext
But, the TableDomainService did not like having a generic being used. I also tried setting the EntityContext but it is read only. I have seen other people use the generic DomainService and the Repository design pattern, but since TableDomainService is what lets me use Azure tables behind the scenes, I think I have to stick with TableDomainService<>. Do I just have to fake the TableDomainService, the TableEntityContext and the TableEntitySet that is returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我从上面的代码中不知道您想要测试的逻辑是什么样的,但您可以尝试将代码(您想要测试的代码)与服务本身分开。
您可以尝试抽象 AncestorDomainService (引入 IAncestorDomainService),然后使用起订量来模拟 IAncestorDomainService。您的逻辑将转移到另一个依赖于 IAncestorDomainService 的类。我已经使用 Linq2Sql 完成了此操作(它似乎具有类似的设计并且也返回 IQueryable)。我不会尝试嘲笑 TableDomainService 的“内部结构”,因为这些东西通常不是为轻松测试而设计的。
I don't know from the code above how the logic you want to test looks like, but you might try seperating your code (the code that you want to test) from the service itself.
You could try to abstract AncestorDomainService (introducing an IAncestorDomainService) and than use moq to mock IAncestorDomainService. Your logic would move to another class that has a dependency to IAncestorDomainService. I've done this with Linq2Sql (which seems to have a similar design and also returns IQueryable). I wouldn't try to mock the 'internals' of TableDomainService because this stuff is usually not designed for easy testing.
如果您有时间的话,最好的解决方案是使您的代码完全可测试。这意味着实际上拥有设置具有已知良好状态的 Azure 实例(真实或本地)所需的脚本。
由于 AncestorDomainService 的全部目的是处理 Azure,因此模拟其基类对于测试有效性观点来说没有多大意义。 (有些人选择优化测试速度而不是有效性,但我认为这是浪费时间。)
THe best solution, if you can afford the time, is to make your code fully testable. That means actually having the scripts necessary to setup an instance of Azure (real or local) with a known good state.
Since the whole point of your AncestorDomainService is to deal with Azure, mocking out its base class doesn't make much sense for a test effectiveness prespective. (Some people choose to optimize for test speed over effectiveness, but I think that's a waste of time.)