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.

Describing a call fails when the ToString() of an argument throws an exception

See original GitHub issue

Say we setup a strict fake:

var a = A.Fake<SomeInterface>(s=>s.Strict()); //A strict fake!
var b = A.Fake<SomeOtherInterface>(); //strictness does not matter...

and then we verify the strict fake was passed as an argument to another mock:

//a.ToString() is called for some reason, however a is a strict fake
A.CallTo(()=>b.SomeMethod(a)).MustHaveHappened()

FakeItEasy throws an Exception:

FakeItEasy.ExpectationException : Call to non configured method "ToString" of strict fake.

Call Stack:

   at FakeItEasy.FakeOptionsBuilderExtensions.<Strict>b__0<T>(IFakeObjectCall call)
   at FakeItEasy.Configuration.BuildableCallRule.Apply(IInterceptedFakeObjectCall fakeObjectCall)
   at FakeItEasy.Core.FakeManager.FakeItEasy.Core.IFakeCallProcessor.Process(IWritableFakeObjectCall fakeObjectCall)
   at Castle.DynamicProxy.AbstractInvocation.Proceed()
   at Castle.Proxies.ObjectProxy_4.ToString()
   at FakeItEasy.Expressions.ArgumentConstraints.EqualityArgumentConstraint.WriteDescription(IOutputWriter writer)
   at FakeItEasy.Expressions.ExpressionCallMatcher.AppendArgumentsListString(StringBuilder result)
   at FakeItEasy.Expressions.ExpressionCallMatcher.ToString()
   at FakeItEasy.Configuration.BuildableCallRule.WriteDescriptionOfValidCall(IOutputWriter writer)
   at FakeItEasy.Configuration.RuleBuilder.MustHaveHappened(Repeated repeatConstraint)

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:67 (59 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Feb 25, 2017

I suggest we release a very simple change now which catches the exception, falling back to the type name. That will solve the general case when the ToString() of any object throws an exception, including this specific bug. After that, #984 may still have value, but we can pursue it for it’s own merits independently of fixing this bug.

That makes sense.

As mentioned in your PR, I think we should prefix the type name with “Faked” if the value is a fake, since this is the default ToString() behavior for fakes.

1reaction
thomaslevesquecommented, Feb 19, 2017

Even if we do some trickery around strictness and System.Object methods, is there still a fundamental flaw here that we need to fix regardless? I.e. we call ToString() on objects when generating messages about them, and we don’t catch exceptions on that call. Perhaps the object itself has a badly behaved ToString method, but we still want to generate the message, rather than have an unhandled exception.

IMO we shouldn’t call ToString() at all on a fake, whether it’s strict or not. In my solution we call IFakeObject.ToString() instead. For non-fakes, I agree that we should catch the exception.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What exactly happens when I throw an exception myself ...
And yes, println just calls toString() on all objects. It'll print the stack trace of the exception. – markspace. Oct 27, 2018 at...
Read more >
How to Throw Exceptions in Java
In the example below, we have created a test method to demonstrate throwing an exception. The toString() method returns a textual representation ...
Read more >
Creating and Throwing Exceptions
Learn about creating and throwing exceptions. Exceptions are used to indicate that an error has occurred while running a program.
Read more >
Error.prototype.toString() - JavaScript - MDN Web Docs - Mozilla
The toString() method of Error instances returns a string representing this error.
Read more >
AWS Lambda function errors in Java
This page describes how to view Lambda function invocation errors for the Java runtime using the Lambda console and the AWS CLI.
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