FakeItEasy 和匹配的匿名类型
FakeItEasy and matching Anonymous types
我无法匹配使用匿名类型的期望。我是 FakeItEasy 的新手,但不是嘲笑我,我想得到一些关于什么是匹配参数的正确方法的指导。我从这个线程 (https://github.com/FakeItEasy/FakeItEasy/issues/532#issuecomment-135968467) 了解到 "predicate can be extracted to a method"。我创建了一个匹配名为 IsMatch 的 Func
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);
}
}
}
@philipwolfe,你的测试结构对我来说很合适,所以我试了一下。
当我更改
时它通过了
{"IsDirty__c", 1}
至
{"IsDirty__c", true}
匹配 ChangeStatus
方法中内置的对象。
我无法匹配使用匿名类型的期望。我是 FakeItEasy 的新手,但不是嘲笑我,我想得到一些关于什么是匹配参数的正确方法的指导。我从这个线程 (https://github.com/FakeItEasy/FakeItEasy/issues/532#issuecomment-135968467) 了解到 "predicate can be extracted to a method"。我创建了一个匹配名为 IsMatch 的 Func
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);
}
}
}
@philipwolfe,你的测试结构对我来说很合适,所以我试了一下。 当我更改
时它通过了{"IsDirty__c", 1}
至
{"IsDirty__c", true}
匹配 ChangeStatus
方法中内置的对象。