Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow calls to faked abstract methods when using callsBaseMethods #16

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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