简体   繁体   中英

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 ?

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.

If leftOverDebtInSystemCurrencyForDate is accessed by your application (other than unit-test), it should remain in your model or be extracted into a service.

Unit-test shouldn't contain any logic for leftOverDebtInSystemCurrencyForDate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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