我有一个如下所示的存储库:
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();
}
}
现在 System under Test 类如下所示:
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();
}
}
我对 CreateCustomer 方法的测试方法如下所示,使用 RhinoMocks。
[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);
}
上面的代码不起作用。我存根InsertOnSubmit()
方法的地方,试图customerToUpdate
从方法中获取实例CreateCustomer()
。如何编写断言以确保CreatedBy
正确CreateDate
设置?