Indicate Fake name (if defined) in failing assertions
See original GitHub issueWhen 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:
- 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()
- 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:
- Created 2 years ago
- Comments:11 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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:
This is a silly example, of course, but I think it illustrates the point.
Maybe the calls should be shown like this instead:
@blairconrad I’d like to finish #1838 first but then perhaps I could also take a look at this one.