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.

A.CallTo(() => ...).MustHaveHappened(2, Times.Exactly) fails for unknown reason after upgrading to netcoreapp3.0

See original GitHub issue

After updating from netcoreapp2.2 to netcoreapp3.0 we’re having some issues with fake it easy. I don’t know whether or not it’s due to fake it easy or netcoreapp. We’re using fake it easy 5.2.0.

This is what we are trying to test:

ILogger logger = kernel.RebindLogger<IExporter>();
kernel.Get<IExporter>().Export();
A.CallTo(() => logger.Log(LogLevel.Warning, A<EventId>._, A<object>._, A<Exception>._, A<Func<object, Exception, string>>._)).MustHaveHappened(2, Times.Exactly);

We just want to make sure that the logger is called two times with LogLevel.Warning.

The relevant part of the result is shown in the attached file below.

We’re getting the two expected calls. The only difference we can see is:

expected: […]ILogger.Log[System.Object][…] actual: […]ILogger[TXS.Analytics.Export.Interface.IExporter].Log[Microsoft.Extensions.Logging.FormattedLogValues][…]

Until we upgraded from netcoreapp2.2 to netcoreapp3.0 this worked perfectly fine.

Maybe someone has an idea or has encountered similar problems?

Many thanks in advance! 😃

  Assertion failed for the following call:
    Microsoft.Extensions.Logging.ILogger.Log`1[System.Object](logLevel: Warning, eventId: <Ignored>, state: <Ignored>, exception: <Ignored>, formatter: <Ignored>)
  Expected to find it twice exactly but didn't find it among the calls:    
    3:  Microsoft.Extensions.Logging.ILogger`1[TXS.Analytics.Export.Interface.IExporter].Log`1[Microsoft.Extensions.Logging.FormattedLogValues](
          logLevel: Warning,
          eventId: 0,
          state: [[{OriginalFormat}, Table/View MICH_GIBT_ES_NICHT does not exist.]],
          exception: NULL,
          formatter: System.Func`3[Microsoft.Extensions.Logging.FormattedLogValues,System.Exception,System.String])
    4:  Microsoft.Extensions.Logging.ILogger`1[TXS.Analytics.Export.Interface.IExporter].Log`1[Microsoft.Extensions.Logging.FormattedLogValues](
          logLevel: Warning,
          eventId: 0,
          state: [[{OriginalFormat}, Table/View V_PFANDOBJEKT has not defined a primary key.]],
          exception: NULL,
          formatter: System.Func`3[Microsoft.Extensions.Logging.FormattedLogValues,System.Exception,System.String])

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
thomaslevesquecommented, Oct 8, 2019

That’s because the method that is called is actually ILogger.Log<FormattedLogvalues>. In your test, you pass A<object>._ for the state argument, so TState is inferred to be object, not FormattedLogValues. So the calls don’t match, since they use a different generic type argument.

Unfortunately, FormattedLogValues is internal, so you can’t just use it as TState… A possible workaround is to check the call like this:

    A.CallTo(logger).Where(call => call.Method.Name == "Log" && call.GetArgument<LogLevel>(0) == LogLevel.Warning)
        .MustHaveHappened(2, Times.Exactly);

It kinda sucks, but I don’t see any better alternative

1reaction
gds03commented, Apr 17, 2021

HI Guys, I have sorted out using object.

ILogger<MyClass> logger = A.Fake<ILogger<MyClass>>();
            A
                .CallTo(logger)
                .Where(call => call.Method.Name == "Log")
                .Invokes(obj =>
                {
                    object o = obj.Arguments.Get<object>("state");
                    string message = o.ToString();
                    loggerString.AppendLine(message);
                });

I wanted to intercept the log method to log to another place in my tests, with the above code it just works OK 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does this MustHaveHappened call on a FakeItEasy ...
When running following test all the assertions fail. ... Expected to find it at least once but no calls were made to the...
Read more >
Assertion - FakeItEasy
The two most common forms of assertion are : MustHaveHappened() (no arguments) asserts that the call was made 1 or more times, and;...
Read more >
Assertion with FakeItEasy - M B A R K's Blog
We're then asserting that the method was called exactly two times by using the MustHaveHappened(1,Times.Exactly) method.
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