priya
priya

Reputation: 858

Best way to make the code Unit Testable

I am using the MVVM architecture and using prism in my application . following is the kind of code i used to set up IresourceManager. Please let me know whether this practice is good or should one go with Dependency Injection and pass the Resourcemanager every time inside the constructor as parameter.

public SapUploadScriptInfo(XDocument scriptDoc) : this()
{
    this.resourceManager = ServiceLocator.Current.GetInstance<IResourceManagerService>();
    this.scriptDoc = scriptDoc;
    this.CreateFromXml(scriptDoc);
}

Now in order to unit test the class which is the best way 1. keep it as it is and unit test this method somewhere else (where it is being used again) 2. using DI approach.

Upvotes: 1

Views: 69

Answers (1)

jlouzado
jlouzado

Reputation: 502

Now in order to unit test the class which is the best way 1. keep it as it is and unit test this method somewhere else (where it is being used again) > 2. using DI approach.

If you try to test this method elsewhere (as part of another function then calls it) then there are several issues :

  • Firstly, you're not really doing unit testing anymore. If you're testing the results of the function call, then you're inadvertently also testing your IResourcemanager class
  • Secondly, test case management will become an issue. As such you want it to be clear where the tests for each function are. But now you have a case where your tests are actually sitting in some other class that happens to use this function.

As such i would recommend going with the dependancy injection approach. It immediately becomes clear what you're trying to do and it's easier to mock IResourcemanager if you had to and pass in an instance of it while running your test suite.

Upvotes: 2

Related Questions