Skip to content

Commit

Permalink
Merge pull request #16 from blairconrad/callsbasemethods
Browse files Browse the repository at this point in the history
Allow calls to faked abstract methods when using callsBaseMethods
  • Loading branch information
tillig authored Jun 4, 2018
2 parents 4fd7796 + b998a8e commit e36cda6
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// This software is part of the Autofac IoC container
// Copyright (c) 2007 - 2008 Autofac Contributors
// Copyright (c) 2007 - 2018 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -103,26 +103,24 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(
/// <returns>A fake object.</returns>
private object CreateFake(TypedService typedService)
{
var fake = Create.Fake(typedService.ServiceType, this.ApplyOptions);

if (this._callsBaseMethods)
{
A.CallTo(fake).CallsBaseMethod();
}

return fake;
return Create.Fake(typedService.ServiceType, this.ApplyOptions);
}

private void ApplyOptions(IFakeOptions options)
{
if (this._strict)
{
options.Strict();
options = options.Strict();
}

if (this._configureFake != null)
{
options.ConfigureFake(x => this._configureFake(x));
options = options.ConfigureFake(x => this._configureFake(x));
}

if (this._callsBaseMethods)
{
options.CallsBaseMethods();
}
}
}
Expand Down
55 changes: 55 additions & 0 deletions test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ public void CanResolveFakesWhichCallsBaseMethods()
}
}

[Fact]
public void CanResolveFakesWhichCallsBaseMethodsAndInvokeAbstractMethod()
{
using (var fake = new AutoFake(callsBaseMethods: true))
{
var bar = fake.Resolve<Bar>();
bar.GoAbstractly();
}
}

[Fact]
public void CanResolveFakesWhichInvokeActionsWhenResolved()
{
Expand Down Expand Up @@ -137,6 +147,49 @@ public void ProvidesInstances()
}
}

[Fact]
public void CallsBaseMethodsOverridesStrict()
{
// A characterization test, intended to detect accidental changes in behavior.
// This is an odd situation, since specifying both strict and callsBaseMethods only makes
// sense when there are concrete methods on the fake that we want to be executed, but we
// want to reject the invocation of any methods that are left abstract on the faked type.
using (var fake = new AutoFake(callsBaseMethods: true, strict: true))
{
var bar = fake.Resolve<Bar>();
bar.Go();
Assert.True(bar.Gone);
}
}

[Fact]
public void CallsBaseMethodsOverridesConfigureFake()
{
// A characterization test, intended to detect accidental changes in behavior.
// Since callsBaseMethods applies globally and configureFake can affect individual
// members, having configureFake override callsBaseMethods may be preferred.
using (var fake = new AutoFake(
callsBaseMethods: true,
configureFake: f => A.CallTo(() => ((Bar)f).Go()).DoesNothing()))
{
var bar = fake.Resolve<Bar>();
bar.Go();
Assert.True(bar.Gone);
}
}

[Fact]
public void ConfigureFakeOverridesStrict()
{
using (var fake = new AutoFake(
strict: true,
configureFake: f => A.CallTo(() => ((Bar)f).Go()).DoesNothing()))
{
var bar = fake.Resolve<Bar>();
bar.Go();
}
}

public abstract class Bar : IBar
{
private bool _gone;
Expand All @@ -155,6 +208,8 @@ public IBar Spawn()
{
throw new NotImplementedException();
}

public abstract void GoAbstractly();
}

public class Baz : IBaz
Expand Down

0 comments on commit e36cda6

Please sign in to comment.