Domain model (display of public properties)

Therefore, when building my domain model, I try to be pragmatic in exposing only what is necessary for driving behavior, but my unit tests require me to expose public getters that are really needed only inside the class. how does everyone handle this? my domain level is only available through my application services level, is this really a big deal? Should I make them internal and provide access to the test project?

any help would be great!

0
source share
4 answers

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

In your situation, it sounds as if your design may not be entirely correct. You are talking about unit tests that should check for private variables, but unit test should be fully defined using public APIs.

Either separate your objects so that the private fields are exposed at some level (making the objects finer grained), or change your tests so as not to need access to these private fields (making your unit tests coarser).

One useful tool is Code Contracts. You can define very fine-grained tests (post-conditions and object invariants defined in terms of a private field) using code contracts and make your block larger. Some of my unit tests no more than call a method and ensure that code contracts do not work.

0
source

I think it is always a bad idea to change the public interface of the class to host the unit test. This is the tail wagging the dog.

If you need to access the internal state of an object to verify it, I would create some extension methods in my testing namespace that make it easy to access the private properties of the object (for example, T GetPropertyValueByName (this is the name of the stringName property).

+2
source

Without looking at the code, it seems like your design might need to be changed to make it more testable. Think about fetching the interface and injecting the dependent injection into your class so that you can set the internal state if you need to. Using this method, you can establish private members during construction. Also try using a mock library. Moq is my favorite.

+1
source

You can make the unit test a library a friend of the domain library and change the private members of your domain classes to internal ones.

0
source

Source: https://habr.com/ru/post/893099/


All Articles