Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

694
Views
How to verify a Dictionary parameter?

I am using the Moq framework.

Given is the following code:

public interface ISomeInterface
{
    SomeResult DoWork( ISomeContainer foo, Dictionary<string, object> bar );
}

[ Test ]
public void SomeTest()
{
    Mock<ISomeInterface> mock = new Mock<ISomeInterface>();
    mock.Setup( m => m.DoWork( It.IsAny<ISomeContainer>(), It.IsAny<Dictionary<string, object>>() ) );

    new Cut( mock ).DoSomething();

    mock.Verify( m => m.DoWork( It.Is<ISomeContainer>( c => c.SomeValue == "foo" ), It.Is<Dictionary<string, object>>( d => ??? ) ) );
}

I know how to verify the properties of an interface parameter (ISomeContainer), but how is this possible with a Dictionary?

I would like to verify that the DoWork method is called with a simple Dictionary that contains only one key-value-pair KeyA + ValueA.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

It.Is anticipates a delegate where the parameter is the current value and the return type bool.

So, in your case: Func<Dictionary<string, object>, bool>

In order to test your assumptions you can create the following helper method:

private static bool AssertBar(Dictionary<string, object> bar)
{
    Assert.Single(bar);
    Assert.Equal("KeyA", bar.Keys.Single());
    Assert.Equal("ValueA", bar.Values.Single());
    
    return true;
}

then you can call the Verify like this:

mock.Verify(
 m => m.DoWork(
    It.Is<ISomeContainer>(c => AssertFoo(c)), 
    It.Is<Dictionary<string, object>>(d => AssertBar(d))),
 Times.Once);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!