域模型(公开公共属性) [英] Domain Model (Exposing Public Properties)

查看:84
本文介绍了域模型(公开公共属性)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在构建域模型时,我尝试只公开引发行为的必要条件,但要务实,但是单元测试要求我公开类中真正需要的公共吸气剂。大家如何处理?我的域层只能通过我的应用程序服务层进行访问,那么真的有什么大不了的吗?我应该将它们设置为内部文件并授予测试项目访问权限吗?

So when building my domain model i am trying to be pragmatic about exposing only whats necessary for driving the behavior but my unit tests are requiring me to expose public getters that are only really needed from within the class. how is everyone handling this? my domain layer is only being accessed through my application services layer, so is it really that big of a deal? should i make them internal and give the test project access?

任何帮助都将非常有用!

any help would be great!

推荐答案

我同意Eric的声明,即单元测试不应影响类API。

I agree with Eric's statement that unit tests should not affect class APIs.

在您的情况下,听起来您的设计可能并不完全正确。您正在谈论需要检查私有变量的单元测试-但应该使用公共API完全定义单元测试。

In your situation, it sounds like your design may not be quite correct. You're talking about unit tests that need to check private variables - but a unit test should be fully defined using public APIs.

要么拆分您的对象,以使私有字段暴露在某一层(使对象更细粒度),或更改测试以使其不需要访问那些私有字段(使单元测试更粗粒度)。

Either split up your objects so that the private fields are exposed at some layer (making the objects more fine-grained), or change your tests to not need access to those private fields (making your unit tests more coarse-grained).

一个有用的工具是代码合同。您可以定义非常精细的-使用代码协定对测试进行细化(后置条件和对象不变式,用私有字段定义),并使单元测试更加粗糙。我的一些单元测试无非就是调用方法并确保不会触发代码合同。

One useful tool is Code Contracts. You can define very fine-grained tests (post-conditions and object invariants, defined in terms of private fields) using Code Contracts, and make your unit testing a little more coarse-grained. Some of my unit tests do nothing more than invoke a method and ensure the Code Contracts don't fire.

这篇关于域模型(公开公共属性)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆