I need help testing the following code
public virtual void Update(T entity)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
int iretries = 0;
bool success = false;
do
{
try
{
this.context.SaveChanges();
success = true;
}
catch (DbUpdateConcurrencyException ex)
{
// Get the current entity values and the values in the database
// as instances of the entity type
var entry = ex.Entries.Single();
var databaseValues = entry.GetDatabaseValues();
// Choose an initial set of resolved values. In this case we
// make the default be the values currently in the database: StoreWins
object resolvedValues = ResolveConcurrency(databaseValues.ToObject());
// Update the original values with the database values and
// the current values with whatever the user choose.
entry.OriginalValues.SetValues(databaseValues);
entry.CurrentValues.SetValues(resolvedValues);
// give up after n retries
if (++iretries == NUMBER_OF_CONC_RETRIES)
throw;
}
catch (Exception)
{
//rethrow
throw;
}
} while (!success);
}
I want to unit test the DbUpdateConcurrencyException branch.
So, one simple test scenario would be:
- Creating a new
DbUpdateConcurrencyException - Mock the
SaveChangesto throw the above exception - Verify that
SaveChangeswas called a number ofNUMBER_OF_CONC_RETRIES - Assert that the
Updatemethod re-throws the exception
In the current state, the above test scenario cannot be tested, I cannot mock the exception to contain a IEnumerable<DbEntityEntry> with a single DbEntityEntry; I cannot mock the GetDatabaseValues(), etc.
A simple solution would be to insert a new layer of abstraction; let's say using an interface to abstract the entire code that currently sits in the catch block, and to provide a mock that does nothing.
But then I would end up in the situation when I would want to test the implementation of that interface, and would end up having the same questions as above. How can I mock the DbUpdateConcurrencyException, the GetDatabaseValues, etc.
I am using moq for mocking.
Thank you for your input