public void CreateInstance_WhenComponentHasRequiredDependencyWithNoParameterValueAndItIsNotSatisfied_Throws() { var dependencyResolver = MockRepository.GenerateMock <IObjectDependencyResolver>(); var objectFactory = new ObjectFactory(dependencyResolver, typeof(ComponentWithRequiredDependencyOnService), new PropertySet()); dependencyResolver.Expect(x => x.ResolveDependency("service", typeof(IService), null)).Return(DependencyResolution.Unsatisfied()); var ex = Assert.Throws <RuntimeException>(() => objectFactory.CreateInstance()); Assert.AreEqual(string.Format("Could not resolve required dependency 'service' of type '{0}'.", typeof(IService)), ex.Message); dependencyResolver.VerifyAllExpectations(); }
public void CreateInstance_WhenComponentHasOptionalDependencyWithNoParameterValueAndItIsNotSatisfied_BuildsTheObjectWithoutTheResolvedDependency() { var dependencyResolver = MockRepository.GenerateMock <IObjectDependencyResolver>(); var objectFactory = new ObjectFactory(dependencyResolver, typeof(ComponentWithOptionalDependencyOnService), new PropertySet()); dependencyResolver.Expect(x => x.ResolveDependency("Service", typeof(IService), null)).Return(DependencyResolution.Unsatisfied()); var component = (ComponentWithOptionalDependencyOnService)objectFactory.CreateInstance(); Assert.IsNull(component.Service); dependencyResolver.VerifyAllExpectations(); }