This is the class with the Import
ed MEF attribute
public class MyManager : IMyManager {
[ImportMany]
public ICollection<Lazy<IContext, IContextMetadata>> Contexts { get; set; }
public IContext { get; set; }
// Implemented from IMyManager interface
public void DoStuff(string name) {
this.Context = GetContext(name);
}
private IContext GetContext(string name) {
return Contexts.Where(c => c.Metadata.Name.Equals(name)).Single().Value;
}
}
I was following this answer to attempt to mock it and test it but I got lost in the actual/expected values, since in my case I need to assert MyManager .Context
was changed.
I'm figuring I have to Mock<IContext>()
and use it to Assert.Equals
, but how do I mock up the whole lazy list?
I typically don't bother mocking an ICollection or IList etc and just use an actual List and similarly for Lazy. I don't think that you gain anything by mocking it out and it ends up being quite hard work with LINQ. Here is an example:
Mock<IContext> context = new Mock<IContext>();
Mock<IContextMetadata> contextMetadata = new Mock<IContextMetadata>();
// Do some setup here
ICollection<Lazy<IContext, IContextMetadata>> contexts = new List<Lazy<IContext, IContextMetadata>>();
Lazy<IContext, IContextMetadata> lazyContext = new Lazy<IContext, IContextMetadata>(() => context.Object, contextMetadata.Object);
contexts.Add(lazyContext)