views:

294

answers:

2

I have such a set of Constructors:

public BusinessObjectContext() :


this(CloudStorageAccount.FromConfigurationSetting("DataConnectionString").TableEndpoint.ToString(),
             CloudStorageAccount.FromConfigurationSetting("DataConnectionString").Credentials) {}

   public BusinessObjectContext(string dataConnectionString) :
        this(CloudStorageAccount.Parse(dataConnectionString).TableEndpoint.ToString(),
             CloudStorageAccount.Parse(dataConnectionString).Credentials) { }

   public BusinessObjectContext(String baseAddress, StorageCredentials credentials) : base(baseAddress, credentials) { } 

However when testing / Mocking I need the object without any of the connection string parameters. How can I do this - preferrably in Moq?

Is this possible at all?

+3  A: 
var myMockBOC = new Mock<BusinessObjectContext>(null, null);

This will pass nulls in for your two parameters.

Another approach would be to create an internal constructor meant for test usage only, and use InternalsVisibleTo to allow your test assembly to use it. Unfortunately this has a big drawback in that if you sign your assemblies, Moq is unable to use the constructor. This is supposed to be addressed in the 4.0 release of Moq though.

womp
+5  A: 

It sounds as if you have a code smell - the constructor is doing too much work. The article includes a set of fixes for such scenarios. Basically the answer is to only perform assignment in constructors, not execute business logic.

Finglas
Hrrm. I see the argument in this concept.On the other hand I wanted to shield the user of the object GenericBusinessObject from thinking about the Data Source Context. If I follow your arguments, then the user of the GenericBusinessObject has to think about it. This is less abtraction. Maybe there is no other way, but something makes me hesitate.
Frank Michael Kraft
Make a factory or use an IOC Conatainer to remove this need for the user to worry about the Data Context.
Finglas