C# Rhino在调用get参数时进行模拟
我有一个如下所示的存储库:C# Rhino在调用get参数时进行模拟,c#,mocking,rhino-mocks,C#,Mocking,Rhino Mocks,我有一个如下所示的存储库: internal class Repository<T> : IRepository<T> where T : class { public virtual ITable GetTable() { return _context.GetTable<T>(); } public virtual void InsertOnSubmit(T entity) { Ge
internal class Repository<T> : IRepository<T> where T : class
{
public virtual ITable GetTable()
{
return _context.GetTable<T>();
}
public virtual void InsertOnSubmit(T entity)
{
GetTable().InsertOnSubmit(entity);
}
public virtual void SubmitChanges()
{
_context.SubmitChanges();
}
}
内部类存储库:IRepository,其中T:class
{
公共虚拟ITable GetTable()
{
返回_context.GetTable();
}
公共虚拟void InsertOnSubmit(T实体)
{
GetTable().InsertOnSubmit(实体);
}
公共虚拟无效提交更改()
{
_context.SubmitChanges();
}
}
现在,测试类下的系统如下所示:
public class CustomerHelper
{
private readonly IRepository<Customer> _customerRepository;
CustomerHelper(IRepository<Customer> customerRepository)
{
_customerRepository = customerRepository;
}
public void CreateCustomer(int createdBy, int customerId)
{
var customerToUpdate = _customerRepository.Get.Single(c => c.Id == customerId)
customerToUpdate.CreatedBy =createdBy;
customerToUpdate.CreateDate = DateTime.Now;
_customerRepository.InsertOnSubmit(customerToUpdate);
_customerRepository.SubmitChanges();
}
}
公共类CustomerHelper
{
私人只读电子存储库(customerRepository);;
CustomerHelper(IRepository customerRepository)
{
_customerRepository=customerRepository;
}
public void CreateCustomer(int createdBy,int customerId)
{
var customerToUpdate=\u customerRepository.Get.Single(c=>c.Id==customerId)
customerToUpdate.CreatedBy=CreatedBy;
customerToUpdate.CreateDate=DateTime.Now;
_customerRepository.InsertOnSubmit(CustomerOutpDate);
_customerRepository.SubmitChanges();
}
}
我使用Rhinomock对CreateCustomer方法的测试方法如下所示
[TestMethod]
public void CreateCustomer()
{
// Arrange
Customer customer = new Customer
{
Id = 1
};
IRepository<Customer> repository = MockRepository.GenerateMock<IRepository<Customer>>();
var customerList = new List<Customer> { customer }.AsQueryable();
repository.Stub(n => n.Get).Return(nonLaborclassificationList);
CustomerHelper helper = new Customer(repository);
helper.CreateCustomer(1, customer.Id);
// Now here I would liek to test whether CreatedBy, CreateDate fields on cutomer are updated correctly. I've tried the below
Customer customerToUpdate;
repository.Stub(c => c.InsertOnSubmit(customer)).WhenCalled(c => { customerToUpdate = n.Arguments[0]; } );
Assert.AreEqual(1, customerToUpdate.CreatedBy);
}
[TestMethod]
public void CreateCustomer()
{
//安排
客户=新客户
{
Id=1
};
IRepository repository=MockRepository.GenerateMock();
var customerList=新列表{customer}.AsQueryable();
repository.Stub(n=>n.Get).Return(nonLaborclassificationList);
CustomerHelper helper=新客户(存储库);
helper.CreateCustomer(1,customer.Id);
//现在,我想测试cutomer上的CreatedBy、CreateDate字段是否正确更新
客户输出日期;
Stub(c=>c.InsertOnSubmit(customer))。当调用时(c=>{customerToUpdate=n.Arguments[0];});
AreEqual(1,customerToUpdate.CreatedBy);
}
上述代码不起作用。我正在stubing
InsertOnSubmit()
方法的位置,试图从CreateCustomer()
方法获取customerToUpdate
实例。如何编写断言以确保CreatedBy
,CreateDate
设置正确?一般策略如下:
helper.CreateCustomer()
InsertOnSubmit()
获取对象的最后一位代码。但是,如果您仍然想这样做,可以使用AssertWasCalled
来帮助:
repository.AssertWasCalled(
x => x.InsertOnSubmit(Arg<Customer>.Matches(c => c.CreateBy))
repository.AssertWasCalled(
x=>x.InsertOnSubmit(Arg.Matches(c=>c.CreateBy))
对于调试,还有一个
GetArgumentsForCallsMadeOn
方法,如果您可以逐步使用调试器,该方法非常有用。代码中有两个问题:
非劳动分类列表
没有定义。我认为应该返回客户列表
存储库的的InsertOnSubmit()
在执行测试操作helper.CreateCustomer(1,customer.Id)
后被存根。因此此存根不起作用。
应在测试操作之前设置存根,就像在操作之前设置一样
当然,如果您想断言
CreatedDate
是否设置正确,您必须为此编写特定的assert
。非劳动分类列表
在存根Get
时返回(这是一个属性还是什么?我在上面显示的存储库类中没有看到它),但在这段代码的任何地方都没有定义。