6286

Domain Model (Exposing Public Properties)

Question:

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 <em>really</em> that big of a deal? should i make them internal and give the test project access?

any help would be great!

Answer1:

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

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 <a href="http://msdn.microsoft.com/en-us/devlabs/dd491992" rel="nofollow">Code Contracts.</a> 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.

Answer2:

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

If you must access internal state of an object to test it, I would create some extension methods in my testing namespace that allow easy access to an object's private properties (e.g., T GetPropertyValueByName(this string propertyName).

Answer3:

Without seeing your code it seems that your design might need to be changed to make it more testable. Think about extracting an interface and implimenting <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="nofollow">dependancy injection</a> to your class so you can set internal state if you need to. Using this method, you can set private members during construction. Also try using a mocking library. <a href="http://code.google.com/p/moq/" rel="nofollow">Moq</a> is my favorite.

Answer4:

You can make the unit test library a <a href="http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx" rel="nofollow">friend</a> of the domain library and change the private members of your domain classes to internal.

Recommend

  • What is the official release update URL for Aptana Studio 3.0?
  • Limit Selectable DOM Checkboxes
  • Program crashes when run outside IDE
  • Get rendered html code in Backing Component from Composite Component
  • record audio in HTML / js without Flash?
  • How Does Navigation in xamarin.forms Works?
  • How can I see a list of all files that are different between two Hg repositories?
  • What does the TypeScript “lib” option really do?
  • Receive mouse move even cursor is outside control
  • .NET video play library which allows to change the playback rate?
  • Implement JwtBearer Authentication in NSwag SwaggerUi
  • Javascript/Jquery runs fast in desktop browsers, but slow in mobile/smartphone browsers…should I spl
  • Request response issues in biztalk
  • Does Apportable support to build library binary (.a/.so)?
  • Access user's phone number on iOS 7
  • How to 'create temp table as select' in Slick?
  • Overlapping controls in Windows XP
  • Can I programmatically choose the Android layout folder?
  • Differences in dis-assembled C code of GCC and Borland?
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • MySQL Order by column = x, column asc?
  • MVC3 Razor - ListBox pre-select not working
  • Switching to Release Build causes runtime error in Web Reference
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JavaScriptCore crash on iOS9
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Controls, properties, events and timers running in design time
  • JSON with duplicate key names losing information when parsed
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Weird JavaScript statement, what does it mean?
  • Jquery - Jquery Wysiwyg return html as a string
  • QuartzCore.framework for Mono Develop
  • Apache 2.4 - remove | delete | uninstall
  • SVN: Merging two branches together
  • Transpose CSV data with awk (pivot transformation)
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • json Serialization in asp
  • Benchmarking RAM performance - UWP and C#