public void WhenDependencyResolverIsSpecifiedItInstance() { var mocker = new AutoMocker(); var service1 = mocker.Get <IService>(); var service2 = mocker.Get <IService2>(); var service3 = mocker.Get <IService3>(); var serviceProvider = mocker.GetMock <IServiceProvider>(); serviceProvider.Setup(x => x.GetService(typeof(IService))).Returns(service1).Verifiable(); serviceProvider.Setup(x => x.GetService(typeof(IService2))).Returns(service2).Verifiable(); serviceProvider.Setup(x => x.GetService(typeof(IService3))).Returns(service3).Verifiable(); DI.Dispose(typeof(ClassWithDependencies).Assembly); DI.Init(typeof(IService).Assembly, builder => { builder.WithProvider(serviceProvider.Object); }); var sut = new ClassWithDependencies(); Assert.AreEqual(service1, sut.Service); Assert.AreEqual(service2, sut.Service2); Assert.AreEqual(service3, sut.Service3); serviceProvider.Verify(); }
public void CreateInstanceWithServiceProvider() { // arrange var services = new ServiceCollection(); services.AddSingleton <ClassWithNoDependencies>(); IServiceProvider serviceProvider = services.BuildServiceProvider(); var factory = new ServiceFactory(); factory.Services = serviceProvider; // act object instance = factory.CreateInstance(typeof(ClassWithDependencies)); // assert Assert.NotNull(instance); Assert.IsType <ClassWithDependencies>(instance); ClassWithDependencies classWithDependencies = (ClassWithDependencies)instance; Assert.NotNull(classWithDependencies.Dependency); }
public void CreateInstanceWithServiceProvider() { // arrange var services = new Mock <IServiceProvider>(); services.Setup(t => t.GetService(It.IsAny <Type>())) .Returns(new ClassWithNoDependencies()); var factory = new ServiceFactory(); factory.Services = services.Object; // act object instance = factory.CreateInstance(typeof(ClassWithDependencies)); // assert Assert.NotNull(instance); Assert.IsType <ClassWithDependencies>(instance); ClassWithDependencies classWithDependencies = (ClassWithDependencies)instance; Assert.NotNull(classWithDependencies.Dependency); }
public void Do_GivenServiceCallResultFalse_ThrowsException() { // arrange var service = new Mock <IService>(); service.Setup(x => x.ServiceCall()).Returns(false); var sut = new ClassWithDependencies(service.Object); // act // assert Assert.Throws <Exception>(() => sut.Do()); }
public void Do_GivenServiceCallResultTrue_DoesNotThrowException() { // arrange var service = new Mock <IService>(); service.Setup(x => x.ServiceCall()).Returns(true); var sut = new ClassWithDependencies(service.Object); // act sut.Do(); // assert service.Verify(x => x.ServiceCall(), Times.Once); }