我有一個資料庫,如下面:犀牛嘲笑調用時獲取參數的
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();
}
}
現在制度下的測試類是像下面這樣:
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()
方法,試圖從CreateCustomer()
方法得到customerToUpdate
實例。我如何編寫斷言來確保CreatedBy
,CreateDate
設置正確?
''當你存根'Get'(是一個屬性還是什麼?我在上面顯示的Repository類中沒有看到它)時會返回'nonLaborclassificationList',但它在此代碼中的任何位置都沒有定義。 –