Skip to content

Commit

Permalink
Fix for issue FakeItEasy#1709
Browse files Browse the repository at this point in the history
  • Loading branch information
asgerhallas committed Nov 23, 2019
1 parent e5ec5d1 commit add3acf
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 12 deletions.
6 changes: 5 additions & 1 deletion docs/advanced-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ Assert.Equal("Baz", calls[1].Method.Name);

## The `FakeManager` object

The `Fake.GetFakeManager` method returns a `FakeManager` object that can be used to get information on the fake and manipulate its call rules.
The `Fake.GetFakeManager` method returns a `FakeManager` object that can be used to get information on the fake and manipulate its call rules.

`Fake.GetFakeManager` throws an exception, if the provided object is not a fake. To test if an object is a fake you can call `Fake.IsFake`
or try get the `FakeManager` with `Fake.TryGetFakeManager` which will return true if the provided object is a fake and also give you the `FakeManager`
for that object via an out parameter.

### Getting the type of the fake

Expand Down
6 changes: 3 additions & 3 deletions src/FakeItEasy/Core/ArgumentValueFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ protected override string GetStringValue(object argumentValue)
{
Guard.AgainstNull(argumentValue, nameof(argumentValue));

var manager = Fake.TryGetFakeManager(argumentValue);

return manager?.FakeObjectDisplayName ?? argumentValue.ToString();
return Fake.TryGetFakeManager(argumentValue, out var manager)
? manager.FakeObjectDisplayName
: argumentValue.ToString();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/FakeItEasy/Core/FakeManager.ObjectMemberRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ private static bool TryHandleEquals(IInterceptedFakeObjectCall fakeObjectCall, F
var argument = fakeObjectCall.Arguments[0];
if (argument is object)
{
var argumentFakeManager = Fake.TryGetFakeManager(argument);
Fake.TryGetFakeManager(argument, out var argumentFakeManager);
bool hasSameFakeManager = ReferenceEquals(argumentFakeManager, fakeManager);
fakeObjectCall.SetReturnValue(hasSameFakeManager);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ public override string ToString()
}
catch (Exception ex) when (!(ex is UserCallbackException))
{
FakeManager manager = Fake.TryGetFakeManager(this.ExpectedValue);
return manager is object
return Fake.TryGetFakeManager(this.ExpectedValue, out var manager)
? manager.FakeObjectDisplayName
: this.ExpectedValue.GetType().ToString();
}
Expand Down
26 changes: 21 additions & 5 deletions src/FakeItEasy/Fake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,31 @@ public static void ClearRecordedCalls(object fakedObject)
/// <summary>
/// Gets the fake manager associated with the proxy, if any.
/// </summary>
/// <param name="fakedObject">The proxy to get the manager from.</param>
/// <returns>The fake manager, or <c>null</c> if <paramref name="fakedObject"/> is not actually a faked object.</returns>
/// <param name="potentialFake">The potential proxy to get the manager from.</param>
/// <param name="fakeManager">The fake manager, or <c>null</c> if <paramref name="potentialFake"/> is not actually a faked object.</param>
/// <returns><c>true</c> if <paramref name="potentialFake"/> is a faked object, else false.</returns>
[DebuggerStepThrough]
[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object", Justification = "The term fake object does not refer to the type System.Object.")]
internal static FakeManager TryGetFakeManager(object fakedObject)
public static bool TryGetFakeManager(object potentialFake, out FakeManager fakeManager)
{
Guard.AgainstNull(fakedObject, nameof(fakedObject));
Guard.AgainstNull(potentialFake, nameof(potentialFake));

fakeManager = FakeManagerAccessor.TryGetFakeManager(potentialFake);
return fakeManager != null;
}

/// <summary>
/// Check if an object is a fake.
/// </summary>
/// <param name="potentialFake">The object to test.</param>
/// <returns><c>true</c> if <paramref name="potentialFake"/> is a faked object, else false.</returns>
[DebuggerStepThrough]
[SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object", Justification = "The term fake object does not refer to the type System.Object.")]
public static bool IsFake(object potentialFake)
{
Guard.AgainstNull(potentialFake, nameof(potentialFake));

return FakeManagerAccessor.TryGetFakeManager(fakedObject);
return TryGetFakeManager(potentialFake, out _);
}
}
}
70 changes: 70 additions & 0 deletions tests/FakeItEasy.Tests/FakeTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using FakeItEasy.Core;
using FluentAssertions;

namespace FakeItEasy.Tests
{
using System;
Expand Down Expand Up @@ -53,5 +56,72 @@ public void ClearRecordedCalls_should_be_guarded()
Expression<Action> call = () => Fake.ClearRecordedCalls(A.Dummy<object>());
call.Should().BeNullGuarded();
}

[Fact]
public void TryGetFakeManager_should_be_guarded()
{
// Arrange

// Act

// Assert
FakeManager _;
Expression<Action> call = () => Fake.TryGetFakeManager(A.Dummy<object>(), out _);
call.Should().BeNullGuarded();
}

[Fact]
public void TryGetFakeManager_return_true_for_existing_fakemanager()
{
// Arrange
var aFake = A.Fake<object>();

// Act
var result = Fake.TryGetFakeManager(aFake, out var manager);

// Assert
result.Should().BeTrue();
manager.Should().NotBe(null);
}

[Fact]
public void TryGetFakeManager_return_false_for_no_existing_fakemanager()
{
// Arrange
var notAFake = new object();

// Act
var result = Fake.TryGetFakeManager(notAFake, out var manager);

// Assert
result.Should().BeFalse();
manager.Should().Be(null);
}

[Fact]
public void IsFake_return_true_for_fake()
{
// Arrange
var aFake = A.Fake<object>();

// Act
var result = Fake.IsFake(aFake);

// Assert
result.Should().BeTrue();
}

[Fact]
public void IsFake_return_false_for_non_fake()
{
// Arrange
var notAFake = new object();

// Act
var result = Fake.IsFake(notAFake);

// Assert
result.Should().BeFalse();
}
}
}

0 comments on commit add3acf

Please sign in to comment.