我是Moq和TDD的新手,我正在尝试做的就是在存储库接口上设置方法。Moq框架Func <T,T>
这是完整的故事。
我有一个名为租客与物业BusinessIdentificationNumber域实体类
public class Tenant:EntityBase<Tenant>,IAggregateRoot
{
...
public string BusinessIdentificationNumber {get;set;}
...
}
接下来我对这个实体,其接口储存库就像
public interface IRepository<T>
{
...
T FindBy(Func<T,bool> func);
...
}
哪里出了问题,我使用域服务其中持有创建租户的规则,就像
public class TenantCreationService:ITenantCreationService
{
public TenantCreationService(IRepository<Tenant> tenantRepository){...}
public void CreateTenant(Tenant tenant)
{
//from here there is call to IRepository<Tenant>.FindBy(funcMethod);
}
}
而在单元测试,我正在测试我嘲笑传递给构造函数库中的TenantCreationService,但我想,以测试功能:
- 当租客BusinessIdentificationNumber存储或会话已经存在,它应该返回。
所以我试图做到这一点像
repositoryMock.Setup(x=>x.FindBy(It.Is<Tenant>(t=>t.BusinessIdentificationNumber
== _tenantInTest.BusinessIdentificationNumber))).Returns(_tenantInTest)
但它不会编译。你知道我想要做什么?
编辑: 当我尝试编译下面
repositoryMock.Setup(e => e.FindBy(t => t.BusinessNumber == _validTenant.BusinessNumber)).Returns(
_validTenant);
我得到异常
Unsupported expression: t => (t.BusinessNumber == value(DP.IPagac.UnitTests.DP.IPagac.Module.TenantManagement.TenantDomainServiceTests)._validTenant.BusinessNumber)
您可以在你的问题的编译错误? – 2012-02-10 13:02:35