question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Indicate Fake name (if defined) in failing assertions

See original GitHub issue

When working with a collection of Fakes it’s sometimes difficult to determine which one of them violated expectations.

I came across two cases when this becomes a problem:

  1. Verifing expectations in a loop:
void TestMethod(IList<IInterface> dependencies)
{
	dependencies[1].Method1();
}

[Test]
public void TestCollection()
{
	var fakes = A.CollectionOfFake<IInterface>(3);

	TestMethod(fakes);

	foreach (var fake in fakes)
	{
		A.CallTo(() => fake.Method1())
			.MustNotHaveHappened();
	}
}

This causes:

FakeItEasy.ExpectationException : 

  Assertion failed for the following call:
    Tests.IInterface.Method1()
  Expected to find it never but found it once among the calls:
    1: Tests.IInterface.Method1()
  1. Using strict Fakes:
void TestMethod(IList<IInterface> dependencies)
{
	foreach (var dependency in dependencies)
		dependency.Method1();
}

[Test]
public void Strict()
{
	var fakes = A.CollectionOfFake<IInterface>(3, o => o.Strict());

	TestMethod(fakes);
}

which results in an error:

FakeItEasy.ExpectationException : Call to unconfigured method of strict fake: Tests.IInterface.Method1().

Named Fakes seem to be an intuitive and convenient way of solving this problem, but unfortunately assigned names are only displayed if a named Fake is used as an argument of a call, so at the moment they cannot help in situations described above.

It would be very helpful if Fake names were also included in error messages, e.g. with Fakes from previous examples named Dependency1 and Dependency0:

FakeItEasy.ExpectationException : 

  Assertion failed for the following call on Dependency1:
    Tests.IInterface.Method1()
  Expected to find it never but found it once among the calls:
    1: Tests.IInterface.Method1()

and

FakeItEasy.ExpectationException : Call to unconfigured method of strict fake Dependency0: Tests.IInterface.Method1().

A new overload for A.CollectionOfFake could also simplify assigning a unique name to each Fake:

var fakes = A.CollectionOfFake<IInterface>(3, (o, i) => o.Named($"Fake{i}"));

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Dec 5, 2021

Hi @PiotrKlecha,

I agree that indicating the fake name in ordered assertion messages would be useful, however I don’t think the suggested format is appropriate. Currently the message indicates on which interface the expected call must happen. If you replace the whole thing with the fake name, you lose that information.

Consider this:

public interface IFoo
{
    int Get();
}

public interface IBar
{
    int Get();
}

public class FooBar : IFoo, IBar
{
    public virtual int Get() => 42;
}

FooBar fake = A.Fake<FooBar>();

//  Assertion failed for the following call:
//    UserQuery+IFoo.Get()
A.CallTo(() => ((IFoo)fake).Get()).MustHaveHappened();

//  Assertion failed for the following call:
//    UserQuery+IBar.Get()
A.CallTo(() => ((IBar)fake).Get()).MustHaveHappened();

This is a silly example, of course, but I think it illustrates the point.

Maybe the calls should be shown like this instead:

Tests.IInterface.Method1() on Dependency1

1reaction
PiotrKlechacommented, May 25, 2021

@blairconrad I’d like to finish #1838 first but then perhaps I could also take a look at this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

assert False vs. assert false - python
Asserting False is a way to force an AssertionError to be thrown. It's a pretty bad technique because you've added explicit code to...
Read more >
Programming With Assertions
When the system runs the assertion, it evaluates Expression1 and if it is false throws an AssertionError with no detail message. The second...
Read more >
Assertions In Selenium Using Junit And TestNG Frameworks
Assertions in Selenium can be handled by pre-defined methods of Junit and TestNG ... Consider a test case to assert the title of...
Read more >
C/C++ Assertions - Visual Studio (Windows)
If that condition is not true, the assertion fails, execution of your program is interrupted, and the Assertion Failed dialog box appears.
Read more >
Python's assert: Debug and Test Your Code Like a Pro
In this tutorial, you'll learn how to use Python's assert statement to document, debug, and test code in development.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found