{
  "$type": "site.standard.document",
  "canonicalUrl": "https://johnnyreilly.com/posts/simple-technique-for-initialising",
  "description": "Refactoring a legacy app includes adding unit tests, but properties with internal setters pose a problem. John explores various approaches.",
  "path": "/posts/simple-technique-for-initialising",
  "publishedAt": "2012-04-16T00:00:00.000Z",
  "site": "at://did:plc:yy3apqjlms24kso7ahn7lbmb/site.standard.publication/3mova7c4nho2b",
  "tags": [
    "automated testing"
  ],
  "textContent": "I was recently working with my colleagues on refactoring a legacy application. We didn't have an immense amount of time available for this but the plan was to try and improve what was there as much as possible. In its initial state the application had no unit tests in place at all and so the plan was to refactor the code base in such a way as to make testing it a realistic proposition. To that end the domain layer was being heavily adjusted and the GUI was being migrated from WebForms to MVC 3. The intention was to build up a pretty solid collection of unit tests. However, as we were working on this we realised we had a problem with properties on our models with internal.aspx>) setters...\n\n\n\nBackground\n\nThe entities of the project in question used an approach which would store pertinent bits of normalised data for read-only purposes in related entities. I've re-read that sentence and realise it's as clear as mud. Here is an example to clarify:\n\nIn the example above you have 2 types of entity: Person and Order. The Order entity makes use of the the Id, FirstName and LastName properties of the Person entity in the properties OrderedById, OrderedByFirstName and OrderedByLastName. For persistence (ie saving to the database) purposes the only necessary Person property is OrderedById identity. OrderedByFirstName and OrderedByLastName are just \"nice to haves\" - essentially present to make implementing the GUI more straightforward.\n\nTo express this behaviour / intention in the object model the setters for OrderedByFirstName and OrderedByLastName are marked as internal. The implication of this is that properties like this can only be initialised within the current assembly - or any explicitly associated \"friend\" assemblies. In practice this meant that internally set properties were only populated when an object was read in from the database. It wasn't possible to set these properties in other assemblies which meant less code was written (<u>a good thing</u>\n\n) - after all, why set a property when you don't need to?\n\nBackground explanation over. It may still be a little unclear but I hope you get the gist.\n\nWhat's our problem?\n\nI was writing unit tests for the controllers in our main web application and was having problems with my arrangements. I was mocking the database calls in my controllers much in the manner that you might expect:\n\nAll looks fine doesn't it? It's not. Because OrderedByFirstName and OrderedByLastName have internal setters we are <u>unable</u>\n\nto initialise them from within the context of our test project. So what to do?\n\nWe toyed with 3 approaches and since each has merits I thought it worth going through each of them:\n\n1. To the MOQumentation Batman!: http://code.google.com/p/moq/wiki/QuickStart! Looking at the MOQ documentation it states the following:\n\n   _Mocking internal types of another project: add the following assembly attributes (typically to the AssemblyInfo.cs) to the project containing the internal types:_\n\n   \n\n   This looked to be exactly what we needed and in most situations it would make sense to go with this. Unfortunately for us there was a gotcha. Certain core shared parts of our application platform were GAC'd. A requirement for GAC-ing an assembly is that it is signed.\n\n   The upshot of this was that if we wanted to use the InternalsVisibleTo approach then we would need to sign our web application test project. We weren't particularly averse to that and initially did so without much thought. It was then we remembered that every assembly referenced by a signed assembly must also be signed as well. We didn't really want to sign our main web application purely for testing purposes. We could and if there weren't viable alternatives we well might have. But it just seemed like the wrong reason to be taking that decision. Like using a sledgehammer to crack a nut.\n\n2. The next approach we took was using mock objects. Instead of using our objects straight we would mock them as below:\n\n   \n\n   Now this approach worked fine but had a couple of snags:\n   - As you can see it's pretty verbose and much less clear to read than it was previously.\n   - It required that we add the virtual keyword to all our internally set properties like so:\n\n     \n\n   - Our standard constructor already initialised the value of our internally set properties. So adding virtual to the internally set properties generated ReSharper warnings aplenty about virtual properties being initialised in the constructor. Fair enough.\n\n   Because of the snags it still felt like we were in nutcracking territory...\n\n3. ... and this took us to the approach that we ended up adopting: a special mocking constructor for each class we wanted to test, for example:\n\n   \n\n   Thanks to the ever lovely Named and Optional Arguments feature of Ccombined with Object Initializers it meant it was possible to write quite expressive, succinct code using this approach; for example:\n\n   \n\n   Here we're calling the mocking constructor to set the internally set properties and subsequently initialising the other properties using the object initialiser mechanism.\n\n   Implementing these custom constructors wasn't a massive piece of work and so we ended up settling on this technique for initialising internal properties.",
  "title": "A Simple Technique for Initialising Properties with Internal Setters for Unit Testing"
}