0

I having trouble matching an expectation that uses anonymous types. I am new to FakeItEasy but not to mocking and would like some guidance on what is the proper way to match arguments. I understand from this thread (https://github.com/FakeItEasy/FakeItEasy/issues/532#issuecomment-135968467) that the "predicate can be extracted to a method". I have created a method that matches a Func<object, bool> signature named IsMatch to hide the reflection (similar to the link#comment included above) and the FakeItEasy argument parser still doesn't pick it up. Here is a failing test. How can I check the anonymous type?

using System;
using System.Collections.Generic;
using FakeItEasy;
using Xunit;

namespace UnitTests
{
    public class Tests
    {
    private Dictionary<string, object> _properties;

    [Fact]
    public void AnonymousTest()
    {
        string id = "123456ABCD";
        string status = "New Status";

        var fake = A.Fake<IRepository>();
        var logic = new BusinessLogic(fake);

        _properties = new Dictionary<string, object>()
        {
            {"Status__c", status},
            {"UpdatedOn__c", DateTime.Today},
            {"IsDirty__c", 1},
        };

        var expectation = A.CallTo(() => fake.UpdateDatabase(id, A<object>.That.Matches(anon => IsMatch(anon))));

        logic.ChangeStatus(id, status);

        expectation.MustHaveHappenedOnceExactly();
    }

    private bool IsMatch(object o)
    {
        foreach (var prop in _properties)
        {
            if (!o.GetType().GetProperty(prop.Key).GetValue(o).Equals(prop.Value))
                return false;
        }

        return true;
    }
}
public interface IRepository
{
    void UpdateDatabase(string id, object fields);
}

public class BusinessLogic
{
    private IRepository _repo;
    public BusinessLogic(IRepository repo)
    {
        _repo = repo;
    }

    public void ChangeStatus(string id, string status)
    {
        var fields = new
        {
            Status__c = status,
            UpdatedOn__c = DateTime.Today,
            IsDirty__c = true
        };
        _repo.UpdateDatabase(id, fields);
    }
}
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
philipwolfe
  • 338
  • 1
  • 7

1 Answers1

0

@philipwolfe, the structure of your test looked right to me, so I tried it out. It passes when I change

{"IsDirty__c", 1}

to

{"IsDirty__c", true}

to match the object built in ChangeStatus method.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111