01.public class Foo02.{03. public Bar Bar;04. 05. public Foo(Bar bar)06. {07. Bar = bar;08. }09.}10. 11.public class Bar12.{13. public IBaz Baz;14. 15. public Bar(IBaz baz)16. {17. Baz = baz;18. }19.}20. 21.public interface IBaz22.{23. String GetString();24.}25. 26.[Test]27.public void when()28.{29. var foo = new MockingContainer<Foo>();30. foo.Arrange<IBaz>(baz => baz.GetString()).Returns("Rawr!");31. 32. var actualResult = foo.Instance.Bar.Baz.GetString();33. 34. Assert.AreEqual("Rawr!", actualResult);35.}Given the above code, a null reference exception is thrown on line 32.
I am using NInject to do constructor dependency injection. I have a large hierarchy of classes that take in their dependencies in their constructors as Foo and Bar do above. I want all of my concrete classes to be instantiated and only have the interfaces get mocked. In the above example, I want a real Bar constructed and a real Foo constructed but I want IBaz to be mocked.
I am aware that in the above scenario I could just manually construct a Foo, passing in a manually constructed Bar, but in my scenario, the dependency hierarchy is large and complex and interfaces wrap all external (not my code calls). Is there an easy way to tell the MockingContainer<Foo> that it should mock all interfaces but not mock any concrete classes (actually instantiate them).
As I said, I am using Ninject to instantiate my classes and it does the right thing everywhere. I understand that the automocking container is backed by Ninject. Is there a way I can bind certain interfaces/classes to JustMock? In the above example I could do something like:
kernel.Bind<IBaz>().To<Mock<IBaz>>()? That way I could just use Ninject to instantiate my object hierarchy, but tell it which interfaces I want it to bind to JustMock mocked instances.