I've a repository like the below:
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();
}
}
Now the System under Test class is like the one below:
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();
}
}
My test method to the CreateCustomer method like the below, using 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);
}
The above code is not working. The place where I am stubbing InsertOnSubmit()
method , trying to get the customerToUpdate
instance from CreateCustomer()
method. How do I write asserts to make sure that CreatedBy
, CreateDate
set properly?
The general strategy is this:
helper.CreateCustomer()
In this case, you probably can just check the first Customer object you created which is stubbed into the repository. The actual code you're testing is using the same object (same reference), so you really don't need the last bit of code where you get the object from InsertOnSubmit()
. However, if you still want to do that you can use AssertWasCalled
to help:
repository.AssertWasCalled(
x => x.InsertOnSubmit(Arg<Customer>.Matches(c => c.CreateBy))
For debugging, there's also a GetArgumentsForCallsMadeOn
method that is useful if you can step through with a debugger.
There are 2 issues in your code:
As Jeff Bridgman says in comment, nonLaborclassificationList
is not defined. I assume customerList
should be returned instead.
InsertOnSubmit()
for repository
is stubbed after test action helper.CreateCustomer(1, customer.Id)
is executed. So this stub does not work.
Stub should be set before test action, as Arrange goes before Act .
And, of course, if you want to assert whether CreatedDate
is set correctly, you have to write particular Assert
for that :) .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.