简体   繁体   English

模型是否可以/应该仅包含用于单元测试的属性/变量?

[英]Can/should model contain properties/variables only for unit testing?

What is common/best practice for data stored in model. 模型中存储的数据的常用/最佳做法是什么。 I'm writing unit tests for calculations and i have a property that returns a value of other properties that are actually used for storing data in database. 我正在编写用于计算的单元测试,并且我有一个属性,该属性返回实际用于在数据库中存储数据的其他属性的值。 In model it is defined like this 在模型中这样定义

    bool IsDebtAndPayCurrencyTheSame;

    public decimal leftOverDebtInSystemCurrencyForDate
    {
        get { return IsDebtAndPayCurrencyTheSame ? debt.allDebt.systemCurrency : Math.Round(debt.leftOverDebt.documentCurrency * Convert.ToDecimal(DebtCurrencyKoefPayoDate), 2); }
    }

and unit test looks something like this 单元测试看起来像这样

        Assert.AreEqual(100, income.detailRow[0].account.leftOverDebtInSystemValueForDate);

So should i leave leftOverDebtInSystemCurrencyForDate in model or copy it's logic to unit test ? 那么我应该在模型中保留leftOverDebtInSystemCurrencyForDate还是将其逻辑复制到单元测试中?

Can Model contain properties just for unit testing ? 模型可以包含仅用于单元测试的属性吗?

Well, there is no restriction. 好吧,没有限制。 You can add whatever properties you want. 您可以添加所需的任何属性。

Should you do that ? 你应该那样做吗?

NO. 没有。 It is not good practice to add such properties. 添加此类属性不是一个好习惯。 Again it's all about how you want to design your classes. 同样,这全都与您要如何设计类有关。 As per rules of abstraction and encapsulations, you should expose only required properties to the class consumers. 根据抽象和封装的规则,您应该只向类使用者公开必需的属性。

Ideally in unit testing also, you should check the end results of the class /method being tested rather than testing internal / private variables. 理想情况下,在单元测试中,也应该检查被测试的类/方法的最终结果,而不是测试内部/私有变量。

Hope this helps. 希望这可以帮助。

If leftOverDebtInSystemCurrencyForDate is completely irrelevant to your application, it should be removed from your code. 如果leftOverDebtInSystemCurrencyForDate与您的应用程序完全无关,则应将其从代码中删除。

If leftOverDebtInSystemCurrencyForDate is accessed by your application (other than unit-test), it should remain in your model or be extracted into a service. 如果您的应用程序访问了leftOverDebtInSystemCurrencyForDate (而不是单元测试),则应将其保留在模型中或提取到服务中。

Unit-test shouldn't contain any logic for leftOverDebtInSystemCurrencyForDate . 单元测试不应为leftOverDebtInSystemCurrencyForDate包含任何逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM