Skip to content

Commit

Permalink
Merge pull request FakeItEasy#971 from blairconrad/with-non-void-retu…
Browse files Browse the repository at this point in the history
…rn-type-matches-void]

Keep WithNonVoidReturnType from matching void
  • Loading branch information
thomaslevesque committed Jan 17, 2017
2 parents 14ba710 + 07ff5ed commit 7b210ca
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 88 deletions.
26 changes: 21 additions & 5 deletions src/FakeItEasy/Configuration/AnyCallCallRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public override string DescriptionOfValidCall
return "Any call with return type {0} to the fake object.".FormatInvariant(this.ApplicableToMembersWithReturnType.FullName);
}

if (this.ApplicableToAllNonVoidReturnTypes)
{
return "Any call with non-void return type to the fake object.";
}

return "Any call made to the fake object.";
}
}
Expand All @@ -37,11 +42,22 @@ public override void UsePredicateToValidateArguments(Func<ArgumentCollection, bo

protected override bool OnIsApplicableTo(IFakeObjectCall fakeObjectCall)
{
return
this.argumentsPredicate(fakeObjectCall.Arguments) &&
(this.ApplicableToMembersWithReturnType == null
|| (this.ApplicableToMembersWithReturnType == fakeObjectCall.Method.ReturnType)
|| (this.ApplicableToAllNonVoidReturnTypes && fakeObjectCall.Method.ReturnType != typeof(void)));
if (!this.argumentsPredicate(fakeObjectCall.Arguments))
{
return false;
}

if (this.ApplicableToMembersWithReturnType != null)
{
return this.ApplicableToMembersWithReturnType == fakeObjectCall.Method.ReturnType;
}

if (this.ApplicableToAllNonVoidReturnTypes)
{
return fakeObjectCall.Method.ReturnType != typeof(void);
}

return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ Imports Xunit
Public Class RefArgumentConstraintTests

Public Interface IFoo
Function Bar(ByRef x As String) As Integer
Function Baz(<Out> ByRef x As String) As Integer
Function Bar(ByRef refParameter As String) As Integer
Function Baz(<Out> ByRef outParameter As String) As Integer
End Interface

<Fact>
Expand Down
71 changes: 71 additions & 0 deletions tests/FakeItEasy.Specs/AnyCallConfigurationSpecs.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
namespace FakeItEasy.Specs
{
using System;
using FakeItEasy.Tests.TestHelpers;
using FluentAssertions;
using Xbehave;
using Xunit;

public static class AnyCallConfigurationSpecs
{
public interface IFoo
{
T Bar<T>() where T : class;

void Baz();
}

[Scenario]
Expand All @@ -27,6 +32,40 @@ public static void WithNonVoidReturnType(IFoo fake)
.x(() => fake.Bar<IFoo>().Should().BeNull());
}

[Scenario]
public static void WithNonVoidReturnTypeAndVoidMethod(
IFoo fake,
bool methodWasIntercepted)
{
"Given a fake with a void method"
.x(() => fake = A.Fake<IFoo>());

"And the fake is configured to set a flag for any non-void return type"
.x(() => A.CallTo(fake).WithNonVoidReturnType().Invokes(() => methodWasIntercepted = true));

"When the void method is called"
.x(() => fake.Baz());

"Then the flag is not set"
.x(() => methodWasIntercepted.Should().BeFalse());
}

[Scenario]
public static void WithNonVoidReturnTypeDescription(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());

"When an assertion is made that a non-void method was called"
.x(() => exception = Record.Exception(() => A.CallTo(fake).WithNonVoidReturnType().MustHaveHappened()));

"Then an exception is thrown"
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()
.WithMessage("*Any call with non-void return type to the fake object.*"));
}

[Scenario]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA2204:Literals should be spelled correctly", MessageId = nameof(IFoo), Justification = "It's an identifier")]
public static void WithReturnType(
Expand All @@ -45,5 +84,37 @@ public static void WithNonVoidReturnType(IFoo fake)
"And the configured method returns a dummy when called with generic argument IFoo"
.x(() => fake.Bar<IFoo>().Should().NotBeNull().And.BeAssignableTo<IFoo>());
}

[Scenario]
public static void WithReturnTypeDescription(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());

"When an assertion is made that a method with a particular return type was called"
.x(() => exception = Record.Exception(() => A.CallTo(fake).WithReturnType<Guid>().MustHaveHappened()));

"Then an exception is thrown"
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()
.WithMessage("*Any call with return type System.Guid to the fake object.*"));
}

[Scenario]
public static void AnyCallDescription(
IFoo fake,
Exception exception)
{
"Given a fake"
.x(() => fake = A.Fake<IFoo>());

"When an assertion is made that any method was called"
.x(() => exception = Record.Exception(() => A.CallTo(fake).MustHaveHappened()));

"Then an exception is thrown"
.x(() => exception.Should().BeAnExceptionOfType<ExpectationException>()
.WithMessage("*Any call made to the fake object.*"));
}
}
}
80 changes: 0 additions & 80 deletions tests/FakeItEasy.Tests/Configuration/AnyCallCallRuleTests.cs

This file was deleted.

1 change: 0 additions & 1 deletion tests/FakeItEasy.Tests/FakeItEasy.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
<Compile Include="ArgumentConstraintManagerExtensions\IsInstanceOfTests.cs" />
<Compile Include="ArgumentConstraintManagerExtensions\IsNullTests.cs" />
<Compile Include="ArgumentConstraintManagerExtensions\StringContainsTests.cs" />
<Compile Include="Configuration\AnyCallCallRuleTests.cs" />
<Compile Include="Configuration\AnyCallConfigurationTests.cs" />
<Compile Include="Configuration\FakeConfigurationExceptionTests.cs" />
<Compile Include="Configuration\FakeConfigurationManagerTests.cs" />
Expand Down

0 comments on commit 7b210ca

Please sign in to comment.