如何使用 LINQ 表达式 return 来自模拟存储库的新对象?
How do I return a new object from a mocked repository using a LINQ Expression?
我正在使用 Rhino Mocks 并创建了一个 Mock Repository。
var salesForcePolicies = MockRepository.GenerateMock<IRepository<SalesForcePolicy>>();
我有一个私有方法 return 来自存储库的 policy
项。
private SalesForcePolicy GetSalesForcePolicyByClientId(decimal clientId)
{
var policy = salesForcePolicies.FindByExp(x => x.IdNumber == clientId).FirstOrDefault();
if (policy == null) throw new AccountUpdaterException("Policy Not Found in Sales Force Lookup Table.");
return policy;
}
我正在尝试将存储库获取到 return 一个新对象,当在 private
方法中调用查询时设置了一个 属性。我不想破坏封装并设置为 public virtual
.
当我写这个的时候编译器在尖叫...
var clientId = decimal.Parse("123456");
salesForcePolicies.Stub(x => x.FindByExp(y => y.IdNumber == clientId)).Return(new SalesForcePolicy { PolicyNumber = "123456" });
如何在查询时将这个模拟存储库获取到 return 一个新对象?
Error: The best overloaded method match for 'Rhino.Mocks.Interfaces.IMethodOptions<System.Linq.IQueryable<Data.Models.Ussi.SalesForcePolicy>>.Return(System.Linq.IQueryable<Data.Models.Ussi.SalesForcePolicy>)' has some invalid arguments.
这个技巧是为表达式参数传入 null
,然后使用 .IgnoreArguments()
。对于 return 创建一个 new List<T>()
并调用 .AsQueryable()
.
[TestMethod]
public void Can_Mock_Sales_Force_Repo()
{
// arrange
var salesForcePolicyRepo = MockRepository.GenerateMock<IRepository<SalesForcePolicy>>();
// act
var clientId = decimal.Parse("123456");
var salesForcePolicy = new SalesForcePolicy { PolicyNumber = "123456" };
IEnumerable<SalesForcePolicy> salesForcePolicies = new List<SalesForcePolicy> { salesForcePolicy };
salesForcePolicyRepo.Stub(x => x.FindByExp(null)).IgnoreArguments().Return(salesForcePolicies.AsQueryable());
var policy = salesForcePolicyRepo.FindByExp(x => x.IdNumber == clientId).FirstOrDefault();
// assert
Assert.IsTrue(policy == salesForcePolicy);
}
我正在使用 Rhino Mocks 并创建了一个 Mock Repository。
var salesForcePolicies = MockRepository.GenerateMock<IRepository<SalesForcePolicy>>();
我有一个私有方法 return 来自存储库的 policy
项。
private SalesForcePolicy GetSalesForcePolicyByClientId(decimal clientId)
{
var policy = salesForcePolicies.FindByExp(x => x.IdNumber == clientId).FirstOrDefault();
if (policy == null) throw new AccountUpdaterException("Policy Not Found in Sales Force Lookup Table.");
return policy;
}
我正在尝试将存储库获取到 return 一个新对象,当在 private
方法中调用查询时设置了一个 属性。我不想破坏封装并设置为 public virtual
.
当我写这个的时候编译器在尖叫...
var clientId = decimal.Parse("123456");
salesForcePolicies.Stub(x => x.FindByExp(y => y.IdNumber == clientId)).Return(new SalesForcePolicy { PolicyNumber = "123456" });
如何在查询时将这个模拟存储库获取到 return 一个新对象?
Error: The best overloaded method match for 'Rhino.Mocks.Interfaces.IMethodOptions<System.Linq.IQueryable<Data.Models.Ussi.SalesForcePolicy>>.Return(System.Linq.IQueryable<Data.Models.Ussi.SalesForcePolicy>)' has some invalid arguments.
这个技巧是为表达式参数传入 null
,然后使用 .IgnoreArguments()
。对于 return 创建一个 new List<T>()
并调用 .AsQueryable()
.
[TestMethod]
public void Can_Mock_Sales_Force_Repo()
{
// arrange
var salesForcePolicyRepo = MockRepository.GenerateMock<IRepository<SalesForcePolicy>>();
// act
var clientId = decimal.Parse("123456");
var salesForcePolicy = new SalesForcePolicy { PolicyNumber = "123456" };
IEnumerable<SalesForcePolicy> salesForcePolicies = new List<SalesForcePolicy> { salesForcePolicy };
salesForcePolicyRepo.Stub(x => x.FindByExp(null)).IgnoreArguments().Return(salesForcePolicies.AsQueryable());
var policy = salesForcePolicyRepo.FindByExp(x => x.IdNumber == clientId).FirstOrDefault();
// assert
Assert.IsTrue(policy == salesForcePolicy);
}