public void TestCrossContextAllowsOverrides() { object viewParent = new object(); object viewChildOne = new object(); object viewChildTwo = new object(); MockContext Parent = new MockContext(viewParent, true); MockContext ChildOne = new MockContext(viewChildOne, true); //Ctr will automatically add to Context.firstcontext. No need to call it manually (and you should not). MockContext ChildTwo = new MockContext(viewChildTwo, true); Parent.ScannedPackages = new string[]{ "strange.unittests.annotated.testCrossContext" }; ChildOne.ScannedPackages = new string[]{ "strange.unittests.annotated.testCrossOverride" }; Parent.Start(); ChildOne.Start(); ChildTwo.Start(); TestCrossContextInterface parentModel = Parent.injectionBinder.GetInstance<TestCrossContextInterface>() as TestCrossContextInterface; //Get the instance from the parent injector (The cross context binding) TestCrossContextInterface childOneModel = ChildOne.injectionBinder.GetInstance<TestCrossContextInterface>() as TestCrossContextInterface; Assert.AreNotSame(childOneModel, parentModel); //The value from getinstance is NOT the same as the cross context value. We have overidden the cross context value locally TestCrossContextInterface childTwoModel = ChildTwo.injectionBinder.GetInstance<TestCrossContextInterface>() as TestCrossContextInterface; Assert.IsNotNull(childTwoModel); Assert.AreNotSame(childOneModel, childTwoModel); //These two are different objects, the childTwoModel being cross context, and childone being the override Assert.AreSame(parentModel, childTwoModel); //Both cross context models are the same parentModel.Value++; Assert.AreEqual(1, childTwoModel.Value); //cross context model should be changed parentModel.Value++; Assert.AreEqual(1000, childOneModel.Value); //local model is not changed Assert.AreEqual(2, parentModel.Value); //cross context model is changed }
public void TestCrossContextImplicit() { object viewParent = new object(); object viewChildOne = new object(); object viewChildTwo = new object(); MockContext Parent = new MockContext(viewParent, true); MockContext ChildOne = new MockContext(viewChildOne, true); //Ctr will automatically add to Context.firstcontext. No need to call it manually (and you should not). MockContext ChildTwo = new MockContext(viewChildTwo, true); Parent.ScannedPackages = new string[]{ "strange.unittests.annotated.testCrossContext" }; Parent.Start(); ChildOne.Start(); ChildTwo.Start(); TestCrossContextInterface parentModel = Parent.injectionBinder.GetInstance<TestCrossContextInterface>() as TestCrossContextInterface; TestCrossContextInterface childOneModel = ChildOne.injectionBinder.GetInstance<TestCrossContextInterface>() as TestCrossContextInterface; Assert.IsNotNull(childOneModel); TestCrossContextInterface childTwoModel = ChildTwo.injectionBinder.GetInstance<TestCrossContextInterface>() as TestCrossContextInterface; Assert.IsNotNull(childTwoModel); Assert.AreSame(childOneModel, childTwoModel); //These two should be the same object Assert.AreEqual(0, parentModel.Value); //start at 0, might as well verify. parentModel.Value++; Assert.AreEqual(1, childOneModel.Value); //child one is updated parentModel.Value++; Assert.AreEqual(2, childTwoModel.Value); //child two is updated }