Skip to content

Commit e36cda6

Browse files
authored
Merge pull request #16 from blairconrad/callsbasemethods
Allow calls to faked abstract methods when using callsBaseMethods
2 parents 4fd7796 + b998a8e commit e36cda6

2 files changed

Lines changed: 64 additions & 11 deletions

File tree

src/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This software is part of the Autofac IoC container
2-
// Copyright (c) 2007 - 2008 Autofac Contributors
2+
// Copyright (c) 2007 - 2018 Autofac Contributors
33
// http://autofac.org
44
//
55
// Permission is hereby granted, free of charge, to any person
@@ -103,26 +103,24 @@ public IEnumerable<IComponentRegistration> RegistrationsFor(
103103
/// <returns>A fake object.</returns>
104104
private object CreateFake(TypedService typedService)
105105
{
106-
var fake = Create.Fake(typedService.ServiceType, this.ApplyOptions);
107-
108-
if (this._callsBaseMethods)
109-
{
110-
A.CallTo(fake).CallsBaseMethod();
111-
}
112-
113-
return fake;
106+
return Create.Fake(typedService.ServiceType, this.ApplyOptions);
114107
}
115108

116109
private void ApplyOptions(IFakeOptions options)
117110
{
118111
if (this._strict)
119112
{
120-
options.Strict();
113+
options = options.Strict();
121114
}
122115

123116
if (this._configureFake != null)
124117
{
125-
options.ConfigureFake(x => this._configureFake(x));
118+
options = options.ConfigureFake(x => this._configureFake(x));
119+
}
120+
121+
if (this._callsBaseMethods)
122+
{
123+
options.CallsBaseMethods();
126124
}
127125
}
128126
}

test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,16 @@ public void CanResolveFakesWhichCallsBaseMethods()
8989
}
9090
}
9191

92+
[Fact]
93+
public void CanResolveFakesWhichCallsBaseMethodsAndInvokeAbstractMethod()
94+
{
95+
using (var fake = new AutoFake(callsBaseMethods: true))
96+
{
97+
var bar = fake.Resolve<Bar>();
98+
bar.GoAbstractly();
99+
}
100+
}
101+
92102
[Fact]
93103
public void CanResolveFakesWhichInvokeActionsWhenResolved()
94104
{
@@ -137,6 +147,49 @@ public void ProvidesInstances()
137147
}
138148
}
139149

150+
[Fact]
151+
public void CallsBaseMethodsOverridesStrict()
152+
{
153+
// A characterization test, intended to detect accidental changes in behavior.
154+
// This is an odd situation, since specifying both strict and callsBaseMethods only makes
155+
// sense when there are concrete methods on the fake that we want to be executed, but we
156+
// want to reject the invocation of any methods that are left abstract on the faked type.
157+
using (var fake = new AutoFake(callsBaseMethods: true, strict: true))
158+
{
159+
var bar = fake.Resolve<Bar>();
160+
bar.Go();
161+
Assert.True(bar.Gone);
162+
}
163+
}
164+
165+
[Fact]
166+
public void CallsBaseMethodsOverridesConfigureFake()
167+
{
168+
// A characterization test, intended to detect accidental changes in behavior.
169+
// Since callsBaseMethods applies globally and configureFake can affect individual
170+
// members, having configureFake override callsBaseMethods may be preferred.
171+
using (var fake = new AutoFake(
172+
callsBaseMethods: true,
173+
configureFake: f => A.CallTo(() => ((Bar)f).Go()).DoesNothing()))
174+
{
175+
var bar = fake.Resolve<Bar>();
176+
bar.Go();
177+
Assert.True(bar.Gone);
178+
}
179+
}
180+
181+
[Fact]
182+
public void ConfigureFakeOverridesStrict()
183+
{
184+
using (var fake = new AutoFake(
185+
strict: true,
186+
configureFake: f => A.CallTo(() => ((Bar)f).Go()).DoesNothing()))
187+
{
188+
var bar = fake.Resolve<Bar>();
189+
bar.Go();
190+
}
191+
}
192+
140193
public abstract class Bar : IBar
141194
{
142195
private bool _gone;
@@ -155,6 +208,8 @@ public IBar Spawn()
155208
{
156209
throw new NotImplementedException();
157210
}
211+
212+
public abstract void GoAbstractly();
158213
}
159214

160215
public class Baz : IBaz

0 commit comments

Comments
 (0)