Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix for #431

  • Loading branch information...
commit 86c0c5f7e1da051c53c9b09f0ffa3a5ca1e71d96 1 parent fe78c8a
@adamralph adamralph authored
View
21 Extras/Source/Autofac.Extras.FakeItEasy/AutoFake.cs
@@ -54,17 +54,24 @@ public class AutoFake : IDisposable
/// <param name="callsDoNothing">
/// <see langword="true" /> to configure fake calls to do nothing when called.
/// </param>
+ /// <param name="builder">The container builder to use to build the container.</param>
/// <param name="onFakeCreated">Specifies an action that should be run over a fake object once it's created.</param>
public AutoFake(
bool strict = false,
bool callsBaseMethods = false,
bool callsDoNothing = false,
- Action<object> onFakeCreated = null)
+ Action<object> onFakeCreated = null,
+ ContainerBuilder builder = null)
{
- var builder = new ContainerBuilder();
- builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource());
+ if (builder == null)
+ {
+ builder = new ContainerBuilder();
+ }
+
+ builder.RegisterSource(new AnyConcreteTypeNotAlreadyRegisteredSource().WithRegistrationsAs(b => b.InstancePerLifetimeScope()));
builder.RegisterSource(new FakeRegistrationHandler(strict, callsBaseMethods, callsDoNothing, onFakeCreated));
this._container = builder.Build();
+ this._container.BeginLifetimeScope();
}
/// <summary>
@@ -100,11 +107,17 @@ public void Dispose()
/// <typeparam name="T">The type of the service.</typeparam>
/// <param name="parameters">Optional parameters</param>
/// <returns>The service.</returns>
- public T Create<T>(params Parameter[] parameters)
+ public T Resolve<T>(params Parameter[] parameters)
{
return this.Container.Resolve<T>(parameters);
}
+ [Obsolete("Use Resolve<T>() instead")]
+ public T Create<T>(params Parameter[] parameters)
+ {
+ return this.Resolve<T>(parameters);
+ }
+
/// <summary>
/// Resolve the specified type in the container (register it if needed)
/// </summary>
View
54 Extras/Tests/Autofac.Extras.Tests.FakeItEasy/AutoFakeFixture.cs
@@ -14,17 +14,17 @@ public void ByDefaultFakesAreNotStrict()
{
using (var fake = new AutoFake())
{
- var foo = fake.Create<Foo>();
+ var foo = fake.Resolve<Foo>();
Assert.DoesNotThrow(() => foo.Go());
}
}
[Test]
- public void CanCreateStrictFakes()
+ public void CanResolveStrictFakes()
{
using (var fake = new AutoFake(strict: true))
{
- var foo = fake.Create<Foo>();
+ var foo = fake.Resolve<Foo>();
Assert.Throws<ExpectationException>(() => foo.Go());
}
}
@@ -34,18 +34,18 @@ public void ByDefaultFakesDoNotCallBaseMethods()
{
using (var fake = new AutoFake())
{
- var bar = fake.Create<Bar>();
+ var bar = fake.Resolve<Bar>();
bar.Go();
Assert.False(bar.Gone);
}
}
[Test]
- public void CanCreateFakesWhichCallsBaseMethods()
+ public void CanResolveFakesWhichCallsBaseMethods()
{
using (var fake = new AutoFake(callsBaseMethods: true))
{
- var bar = fake.Create<Bar>();
+ var bar = fake.Resolve<Bar>();
bar.Go();
Assert.True(bar.Gone);
}
@@ -56,31 +56,31 @@ public void ByDefaultFakesRespondToCalls()
{
using (var fake = new AutoFake())
{
- var bar = fake.Create<IBar>();
+ var bar = fake.Resolve<IBar>();
var result = bar.Spawn();
Assert.NotNull(result);
}
}
[Test]
- public void CanCreateFakesWhichDoNotRespondToCalls()
+ public void CanResolveFakesWhichDoNotRespondToCalls()
{
using (var fake = new AutoFake(callsDoNothing: true))
{
- var bar = fake.Create<IBar>();
+ var bar = fake.Resolve<IBar>();
var result = bar.Spawn();
Assert.Null(result);
}
}
[Test]
- public void CanCreateFakesWhichInvokeActionsWhenCreated()
+ public void CanResolveFakesWhichInvokeActionsWhenResolved()
{
- object createdFake = null;
- using (var fake = new AutoFake(onFakeCreated: obj => createdFake = obj))
+ object resolvedFake = null;
+ using (var fake = new AutoFake(onFakeCreated: obj => resolvedFake = obj))
{
- var bar = fake.Create<IBar>();
- Assert.AreSame(bar, createdFake);
+ var bar = fake.Resolve<IBar>();
+ Assert.AreSame(bar, resolvedFake);
}
}
@@ -92,7 +92,7 @@ public void ProvidesInstances()
var bar = A.Fake<IBar>();
fake.Provide(bar);
- var foo = fake.Create<Foo>();
+ var foo = fake.Resolve<Foo>();
foo.Go();
A.CallTo(() => bar.Go()).MustHaveHappened();
@@ -111,6 +111,30 @@ public void ProvidesImplementations()
}
}
+ [Test]
+ public void ByDefaultAbstractTypesAreResolvedToTheSameSharedInstance()
+ {
+ using (var fake = new AutoFake())
+ {
+ var bar1 = fake.Resolve<IBar>();
+ var bar2 = fake.Resolve<IBar>();
+
+ Assert.AreSame(bar1, bar2);
+ }
+ }
+
+ [Test]
+ public void ByDefaultConcreteTypesAreResolvedToTheSameSharedInstance()
+ {
+ using (var fake = new AutoFake())
+ {
+ var baz1 = fake.Resolve<Baz>();
+ var baz2 = fake.Resolve<Baz>();
+
+ Assert.AreSame(baz1, baz2);
+ }
+ }
+
public interface IBar
{
bool Gone { get; }
Please sign in to comment.
Something went wrong with that request. Please try again.