MVP 模式:将数据库依赖与演示者分离?
每当我尝试实际对演示者和模拟视图进行单元测试时,我最终都会遇到太多的数据库依赖项
public EditAccount(IAccountEditPage _view, ISession _session, IResponse _response)
{
}
public void view_SaveUser()
{
//Class that takes the view's data and persists it to DB
}
显然我无法为此演示者编写单元测试,因为我有一个具体的使用具有强大数据库依赖项的模型类。
我该如何消除对数据库的依赖,而不用构造函数注入演示器中涉及数据库的每个类?我不想每次在我拥有的每个视图中都这样做。
我正在使用最小起订量,如果有帮助的话。
编辑:另外我应该提到“view_SaveUser”中的代码非常精简,不是直接数据库访问或类似的东西。通常只有几行。 AFAIK,我并没有超出演示者的范围。
Whenever I try to actually unit test a presenter and a mocked view, I end up running into too many database dependencies
public EditAccount(IAccountEditPage _view, ISession _session, IResponse _response)
{
}
public void view_SaveUser()
{
//Class that takes the view's data and persists it to DB
}
Obviously I can't write unit tests for this presenter because I have a concretion of using my model class that has a strong database dependency.
How am I supposed to removed the dependency on the database without constructor injecting every class that touches the database in my presenter? I don't want to do this every time in every view I have.
I'm using moq, if it helps.
Edit : Also I should mention that the code in "view_SaveUser" is very lean and isn't direct database access or anything like that. It's usually only a few lines. I'm not overstepping the scope of the presenter, AFAIK.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不想在构造函数上注入实例,则另一个选择是使用 IoC 框架作为 Spring 使用 setter 注入.Net 或 Castle Windsor 注入依赖项。
这样做,您只需要在框架配置上指定哪些类用于实际代码和测试项目,依赖项将自动注入,您将避免使用构造函数。
If you don't want to inject the instances on the constructor another option you have is using a setter injection using a IoC framework as Spring.Net or Castle Windsor to inject the dependencies.
Doing this, you would only need to specify on the framework configuration which classes are used for real code and for test project, dependencies would be automatically injected and you would avoid having to use the contructor.