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.

Identify precise mismatch when a complex argument constraint fails to match an argument

See original GitHub issue

From http://stackoverflow.com/a/43781563/131809

Consider the following assertion

var fake = A.Fake<ICustomerService>();

A.CallTo(() => fake.Add(
    A<Customer>.That.Matches(c =>
        c.Property == "123" &&
        c.OtherProperty == "345" &&
        c.AnotherProperty == "456" &&
        c.YetAnotherProperty == "567"
    )
)).MustHaveHappened();

This gets a bit messy.

If c.AnotherProperty is infact “890” my unit test will not tell me. It won’t tell me that AnotherProperty is wrong. It’ll just fail (correctly)

To work around this, I quite often do this:

var fake = A.Fake<ICustomerService>();

Customer addedCustomer;
A.CallTo(() => fake.Add(A<Customer>._))
    .Invokes(c => addedCustomer = c.GetArgument<Customer>(0));

Assert.IsNotNull(addedCustomer);
Assert.AreEqual("123", addedCustomer.Property);
Assert.AreEqual("345", addedCustomer.OtherProperty);
Assert.AreEqual("456", addedCustomer.AnotherProperty);
Assert.AreEqual("567", addedCustomer.YetAnotherProperty);

I guess it’d be nice if the exception MustHaveHappened throws if the matcher does not validate, somehow includes which property or properties didn’t match

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:26 (21 by maintainers)

github_iconTop GitHub Comments

2reactions
blairconradcommented, May 4, 2017

@alexjamesbrown, thanks for making this.

I may be missing something, but are not the calls output when MustHaveHappened fails to match? e.g.

Assertion failed for the following call:
    FakeItEasyQuestionsVS2015.MustHaveHappened+ICustomerService.Add(c: <c => ((((c.Property == "123") AndAlso (c.OtherProperty == "345")) AndAlso (c.AnotherProperty == "456")) AndAlso (c.YetAnotherProperty == "567"))>)
  Expected to find it at least once but found it #0 times among the calls:
    1: FakeItEasyQuestionsVS2015.MustHaveHappened+ICustomerService.Add(c: FakeItEasyQuestionsVS2015.MustHaveHappened+Customer)

If you can’t tell what’s wrong, have you considered augmenting the Customer rendering? Either a ToString or an ArgumentValueFormatter like

public class CustomerFormatter : ArgumentValueFormatter<Customer>
{
    protected override string GetStringValue(Customer c)
    {
        return
            $"Property: {c.Property}, OtherProperty: {c.OtherProperty}, YetAnotherProperty: {c.YetAnotherProperty}, AnotherProperty: {c.AnotherProperty}";
    }
}

yields

FakeItEasy.ExpectationException : 

  Assertion failed for the following call:
    FakeItEasyQuestionsVS2015.ICustomerService.Add(c: <c => ((((c.Property == "123") AndAlso (c.OtherProperty == "345")) AndAlso (c.AnotherProperty == "456")) AndAlso (c.YetAnotherProperty == "567"))>)
  Expected to find it at least once but found it #0 times among the calls:
    1: FakeItEasyQuestionsVS2015.ICustomerService.Add(c: Property: 123, OtherProperty: 345, YetAnotherProperty: 567, AnotherProperty: 890)
1reaction
blairconradcommented, May 16, 2017

Given the complexity of implementing a solution where the particular failing clause is pinpointed, as well as the potential expansion of the error messages (as we’d have to indicate for each argument of each call which clause of the matcher failed), I propose that we close this wont-fix.

Objections?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Constraints - Configuration Language | Terraform
Type constructors are unquoted symbols followed by a pair of parentheses, which contain an argument that specifies more information about the type.
Read more >
Practical heuristics to improve precision for erroneous ...
Any development or comprehension effort is set back by questioning why a particular expression was passed to a particular parameter even though the...
Read more >
FIO47-C. Use valid format strings
Mismatches between arguments and conversion specifications may result in undefined behavior. Compilers may diagnose type mismatches in formatted output function ...
Read more >
elm - TYPE MISMATCH - This function cannot handle the ...
You are constructing a Model value by only specifying two parameters ( Model key TopPage ). The value of that expression does not...
Read more >
Error conditions in Databricks
Databricks Runtime and Databricks SQL. AGGREGATE_FUNCTION_WITH_NONDETERMINISTIC_EXPRESSION. SQLSTATE: none assigned. Non-deterministic ...
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