Skip to content

Commit

Permalink
Updated to DNX beta8 and added additional unit tests from ASP.NET dep…
Browse files Browse the repository at this point in the history
…endency injection repository (will be replaced with test package when available).
  • Loading branch information
alexmg committed Oct 16, 2015
1 parent 13fe2e2 commit 40c6282
Show file tree
Hide file tree
Showing 43 changed files with 993 additions and 5 deletions.
1 change: 1 addition & 0 deletions NuGet.Config
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<add key="NuGet v3" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<disabledPackageSources>
<add key="AspNetVNext" value="true" />
<add key="AspNetMaster" value="true" />
<add key="NuGet v3" value="true" />
</disabledPackageSources>
Expand Down
4 changes: 2 additions & 2 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ Remove-PathVariable "*Program Files\Microsoft DNX\DNVM*"
Install-Dnvm

# Install DNX
dnvm install $dnxVersion -r CoreCLR -NoNative -Unstable
dnvm install $dnxVersion -r CLR -NoNative -Unstable
dnvm install $dnxVersion -r CoreCLR -NoNative
dnvm install $dnxVersion -r CLR -NoNative
dnvm use $dnxVersion -r CLR

# Package restore
Expand Down
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"sdk": { "version": "1.0.0-beta8-15736" },
"sdk": { "version": "1.0.0-beta8" },
"sources": ["src", "test", "samples"]
}
2 changes: 1 addition & 1 deletion src/Autofac.Extensions.DependencyInjection/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"dependencies": {
"Autofac": "4.0.0-beta8-*",
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-beta8-*"
"Microsoft.Framework.DependencyInjection.Abstractions": "1.0.0-beta8"
},
"frameworks": {
"dnx451": { },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Framework.DependencyInjection.Tests.Fakes;
using Xunit;

namespace Microsoft.Framework.DependencyInjection.Tests
{
public abstract class AllContainerTestsBase
{
protected abstract IServiceProvider CreateContainer();

[Fact]
public void SingleServiceCanBeResolved()
{
var container = CreateContainer();

var service = container.GetService<IFakeService>();

Assert.NotNull(service);
Assert.Equal("FakeServiceSimpleMethod", service.SimpleMethod());
}

[Fact]
public void ServiceInstanceCanBeResolved()
{
var container = CreateContainer();

var service = container.GetService<IFakeServiceInstance>();

Assert.NotNull(service);
Assert.Equal("Instance", service.SimpleMethod());
}

[Fact]
public void TransientServiceCanBeResolved()
{
var container = CreateContainer();

var service1 = container.GetService<IFakeService>();
var service2 = container.GetService<IFakeService>();

Assert.NotNull(service1);
Assert.NotEqual(service1, service2);
}

[Fact]
public void SingleServiceCanBeIEnumerableResolved()
{
var container = CreateContainer();

var services = container.GetService<IEnumerable<IFakeService>>();

Assert.NotNull(services);
Assert.Equal(1, services.Count());
Assert.Equal("FakeServiceSimpleMethod", services.Single().SimpleMethod());
}

[Fact]
public void MultipleServiceCanBeIEnumerableResolved()
{
var container = CreateContainer();

var services = container.GetService<IEnumerable<IFakeMultipleService>>();

var results = services.Select(x => x.SimpleMethod()).ToArray();

Assert.NotNull(results);
Assert.Equal(2, results.Count());
Assert.Contains("FakeOneMultipleServiceAnotherMethod", results);
Assert.Contains("FakeTwoMultipleServiceAnotherMethod", results);
}

[Fact]
public void OuterServiceCanHaveOtherServicesInjected()
{
var container = CreateContainer();

var service = container.GetService<IFakeOuterService>();

string singleValue;
string[] multipleValues;
service.Interrogate(out singleValue, out multipleValues);

Assert.NotNull(service);
Assert.Equal(2, multipleValues.Count());
Assert.Contains("FakeServiceSimpleMethod", singleValue);
Assert.Contains("FakeOneMultipleServiceAnotherMethod", multipleValues);
Assert.Contains("FakeTwoMultipleServiceAnotherMethod", multipleValues);
}

[Fact]
public void FactoryServicesCanBeCreatedByGetService()
{
// Arrange
var container = CreateContainer();

// Act
var service = container.GetService<IFactoryService>();

// Assert
Assert.Equal(42, service.Value);
Assert.NotNull(service.FakeService);
}

[Fact]
public void FactoryServicesAreCreatedAsPartOfCreatingObjectGraph()
{
// Arrange
var container = CreateContainer();

// Act
var service1 = container.GetService<ServiceAcceptingFactoryService>();
var service2 = container.GetService<ServiceAcceptingFactoryService>();

// Assert
Assert.Equal(42, service1.TransientService.Value);
Assert.NotNull(service1.TransientService.FakeService);

Assert.Equal(42, service2.TransientService.Value);
Assert.NotNull(service2.TransientService.FakeService);

Assert.NotNull(service1.ScopedService.FakeService);

// Verify scoping works
Assert.NotSame(service1.TransientService, service2.TransientService);
Assert.Same(service1.ScopedService, service2.ScopedService);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Tests.Fakes;

namespace Microsoft.Framework.DependencyInjection.Tests
{
public class AutofacContainerTests : ScopingContainerTestBase
{
protected override IServiceProvider CreateContainer()
{
var builder = new ContainerBuilder();

builder.Populate(TestServices.DefaultServices());

IContainer container = builder.Build();
return container.Resolve<IServiceProvider>();
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public abstract class AbstractClass
{
public AbstractClass()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class AnotherClass
{
private readonly IFakeService _fakeService;

public AnotherClass(IFakeService fakeService)
{
_fakeService = fakeService;
}

public string LessSimpleMethod()
{
return "[" + _fakeService.SimpleMethod() + "]";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class AnotherClassAcceptingData
{
private readonly IFakeService _fakeService;
private readonly string _one;
private readonly string _two;

public AnotherClassAcceptingData(IFakeService fakeService, string one, string two)
{
_fakeService = fakeService;
_one = one;
_two = two;
}

public string LessSimpleMethod()
{
return string.Format("[{0}] {1} {2}", _fakeService.SimpleMethod(), _one, _two);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class ClassWithAmbiguousCtors
{
public ClassWithAmbiguousCtors(string data)
{
}

public ClassWithAmbiguousCtors(IFakeService service, string data)
{
}

public ClassWithAmbiguousCtors(IFakeService service, int data)
{
}

public ClassWithAmbiguousCtors(IFakeService service, string data1, int data2)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection
{
public class ClassWithInternalConstructor
{
internal ClassWithInternalConstructor()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class ClassWithNestedReferencesToProvider : IDisposable
{
private IServiceProvider _serviceProvider;
private ClassWithNestedReferencesToProvider _nested;

public ClassWithNestedReferencesToProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_nested = new ClassWithNestedReferencesToProvider(_serviceProvider, 0);
}

private ClassWithNestedReferencesToProvider(IServiceProvider serviceProvider, int level)
{
_serviceProvider = serviceProvider;
if (level > 1)
{
_nested = new ClassWithNestedReferencesToProvider(_serviceProvider, level + 1);
}
}

public void Dispose()
{
_nested?.Dispose();
(_serviceProvider as IDisposable)?.Dispose();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class ClassWithOptionalArgsCtor
{
public ClassWithOptionalArgsCtor(string whatever = "BLARGH")
{
Whatever = whatever;
}

public string Whatever { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class ClassWithPrivateCtor
{
private ClassWithPrivateCtor()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection
{
public class ClassWithProtectedConstructor
{
internal ClassWithProtectedConstructor()
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class ClassWithStaticCtor
{
static ClassWithStaticCtor()
{

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace Microsoft.Framework.DependencyInjection.Tests.Fakes
{
public class ClassWithThrowingCtor
{
public ClassWithThrowingCtor(IFakeService service)
{
throw new Exception(nameof(ClassWithThrowingCtor));
}
}
}
Loading

0 comments on commit 40c6282

Please sign in to comment.