From e875bf03a8915dc996f22ddac93d08888ad23e57 Mon Sep 17 00:00:00 2001 From: Blair Conrad Date: Mon, 4 Jun 2018 09:46:20 -0400 Subject: [PATCH 1/2] Add option interaction characterization tests --- .../AutoFakeFixture.cs | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs b/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs index 2e06c8e..bd58eda 100644 --- a/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs +++ b/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs @@ -137,6 +137,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.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.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.Go(); + } + } + public abstract class Bar : IBar { private bool _gone; From b998a8ea61354b8afa92a00656a2e17fcacf3f14 Mon Sep 17 00:00:00 2001 From: Blair Conrad Date: Mon, 4 Jun 2018 09:51:44 -0400 Subject: [PATCH 2/2] Support invocation of abstract members when callsBaseMethods is set --- .../FakeRegistrationHandler.cs | 20 +++++++++---------- .../AutoFakeFixture.cs | 12 +++++++++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs b/src/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs index 402845a..a3f9ff7 100644 --- a/src/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs +++ b/src/Autofac.Extras.FakeItEasy/FakeRegistrationHandler.cs @@ -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 @@ -103,26 +103,24 @@ public IEnumerable RegistrationsFor( /// A fake object. 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(); } } } diff --git a/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs b/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs index bd58eda..0341257 100644 --- a/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs +++ b/test/Autofac.Extras.FakeItEasy.Test/AutoFakeFixture.cs @@ -89,6 +89,16 @@ public void CanResolveFakesWhichCallsBaseMethods() } } + [Fact] + public void CanResolveFakesWhichCallsBaseMethodsAndInvokeAbstractMethod() + { + using (var fake = new AutoFake(callsBaseMethods: true)) + { + var bar = fake.Resolve(); + bar.GoAbstractly(); + } + } + [Fact] public void CanResolveFakesWhichInvokeActionsWhenResolved() { @@ -198,6 +208,8 @@ public IBar Spawn() { throw new NotImplementedException(); } + + public abstract void GoAbstractly(); } public class Baz : IBaz