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 way to define a collection in argument constraint

See original GitHub issue

Would be nice a new argument matcher for a collection of values, that can be good if we are testing some method that process an argument list

Something like this IsInOrderFromColection

    public interface IDep { Double(int n);  }

     var mock = A.Fake<IDep>();
     A.CallTo(() => mock.Double(A<int>._)).Returns(0);

     var numbers = new[] { 2, 4, 5 };
     foreach (var n in numbers)
        mock.Double(n);

      A.CallTo(() => 
         mock.Double(A<int>.That.IsInOrderFromColection(2, 4, 5) ))
         .MustHaveHappened();

it should verify if the Double method is called with 2, 4 and 5 in sequence,

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Mar 14, 2019

@blairconrad sorry, my example was wrong, I fixed it. current needs to be reassigned with the result of Then in the loop body.

1reaction
blairconradcommented, Mar 14, 2019

@lucasteles, I understand your reticence to include a loop in your test, but it might not be hard to hide it within a method. Hmm. Lemme try something.

I’m back. I hacked this up:

public class MustHaveHappenedForEachArgument
{
    public interface IDep { int Double(int n); }

    [Test]
    public void T()
    {
        var mock = A.Fake<IDep>();
        A.CallTo(() => mock.Double(A<int>._)).Returns(0);

        var numbers = new[] { 2, 4, 5 };
        foreach (var n in numbers)
        {
            mock.Double(n);
        }

        AssertWasCalledWithEach(n => () => mock.Double(n), numbers);
    }

    private static void AssertWasCalledWithEach(Func<int, Expression<Func<int>>> callSpecificationFactory, IEnumerable<int> args)
    {
        var current = NullOrderableCallAssertion.Default;
        foreach (var arg in args)
        {
            current = current.Then( // originally did not assign - thanks, @thomaslevesque 	
                A.CallTo(callSpecificationFactory(arg)).MustHaveHappened());
        }
    }

    private class NullOrderableCallAssertion : IOrderableCallAssertion
    {
        public static IOrderableCallAssertion Default { get; } = new NullOrderableCallAssertion();

        private NullOrderableCallAssertion() { }

        public IOrderableCallAssertion Then(UnorderedCallAssertion nextAssertion)
        {
            return nextAssertion;
        }
    }
}

It’s not perfect, but maybe makes things more palatable for you? I tried fully genericizing the method, but the compiler could not figure out the signature.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating a method with a Set collection argument that ...
For this I have created some method public static void process(Set<ConstraintViolation<RegisterFormDTO>> validates) { ... }.
Read more >
Argument constraints
Argument constraints. When configuring and asserting calls in FakeItEasy, the arguments of the call can be constrained so that only calls to the...
Read more >
Constraints on type parameters - C# Programming Guide
Constraints specify the capabilities and expectations of a type parameter. Declaring those constraints means you can use the operations and ...
Read more >
Chapter 3. Declaring and validating method constraints
In the remainder of this chapter you will learn how to declare parameter and return value constraints and how to validate them using...
Read more >
C# Generic Parameter Constraints
Enforcing constraints on specific type parameters allows you to increase the number of allowable operations and method calls.
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