chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!
0 votes
Hello
I am using MEF to resolve the requied Implementation. :)

Following is the Factory class which resolves the required implementaion. Static method "GetLocalisationProviderInstance()" get a appropriate implementaion.

Do I really need to test this? :?
How do I unit test this using typemock? :(





----CODE----
public class LocalisationProviderFactory
{
[ImportMany]
Lazy<ILocalisation, IDictionary<string>>[] LocalisationMainService { get; set; }

public LocalisationProviderFactory()
{
var Catalog = new AggregateCatalog();
Catalog.Catalogs.Add(new DirectoryCatalog(@".\Extensions"));
var Container = new CompositionContainer(Catalog);
Container.ComposeParts(this);
}


public virtual List<ILocalisation> GetLocalisationService(string Type)
{
List<ILocalisation> FilteredLocalisationService = new List<ILocalisation>();
foreach (var LocalisationObj in LocalisationMainService)
{
if (LocalisationObj.Metadata["Type"].ToString().ToUpper() == Type)
FilteredLocalisationService.Add(LocalisationObj.Value);
}
return FilteredLocalisationService;
}

public static ILocalisation GetLocalisationProviderInstance()
{
string Option = ConfigurationSettings.AppSettings["LocalisationProviderName"].ToString();

LocalisationProviderFactory CF = new LocalisationProviderFactory();
List<ILocalisation> objLocalisation = CF.GetLocalisationService(Option.ToUpper());
if (objLocalisation.Count == 0)
{
//No Implementation Found
return null;
}
else
{
return (ILocalisation)objLocalisation[0];
}
}

}
----CODE------
asked by (3.5k points)

2 Answers

0 votes
Hi Sudhir,

This is an interesting question. The case is an external dependency in the ILocalisation dictionary, which is provided by MEF, rather than by a standard function call or constructor. When testing the factory method we would of course like to isolate this dependency in MEF. I would try to fake the getter of the LocalisationMainService property to return a dictionary with test data, instead of trusting MEF to return it. Something like:
// create test data according to what you would like to test
var localisation1 = ...
var localisation2 = ...

// create the object under test. Because we are testing an instance method on a factory class, you can inject the object under test using Swap.NextInstance:
var factoryUnderTest = new LocalisationProviderFactory();
Isolate.Swap.NextInstance<LocalisationProviderFactory>().With(factoryUnderTest);
// return the test data instead of the MEF import
Isolate.WhenCalled(() => factoryUnderTest.LocalisationMainService).WillReturn(new[] {localisation1, localisation2});

// call the method under test
LocalisationProviderFactory.GetLocalisationService("MyType");

// assert results according to test data/requirements


Hope this helps!

Doron
Typemock Support
answered by (16.5k points)
0 votes
Thanks doron. This wil help. 8)
answered by (3.5k points)
...