I have a method that adds an object to a database, processes that object, and then updates the same object in the database if the processing was successful. The processing is a very critical part of the system (payment) so I want to make sure that if the system crashes/power cut during the processing, we still have the initial add that we can then process again.
public void CreateAndProcess(){
var myObj= new myObj
{
// Set up myObj here
};
db.Add(myObj);
db.SaveChanges();
// Process and update myObj here
db.Update(myObj);
db.SaveChanges();
}
I then want to unit test the CreateAndProcess method to ensure that the Add and Update methods were called with myObj containing the correct values.
// addedObj and updatedObj are objects I define in my test that should match.
_store.AssertWasCalled(store => store.Add(Arg<myObj>.Matches(mo => Equal(mo, addedObj))),
option => option.Repeat.Once()); // Initial add to db
_store.AssertWasCalled(store => store.Update(Arg<myObj>.Matches(mo => Equal(mo, updatedObj))),
option => option.Repeat.Once()); // Update in db after processed
private static bool Equal(myObj s1, myObj s2)
{
// Used to match a subset of properties I care about
Assert.That(s1.myProp, Is.EqualTo(s2.myProp), "Unexpected argument");
return true;
}
The issue is that the first AssertWasCalled is using the final state of the object, after it is updated, rather than it's value at the time the Add() function was called.
How can I test the state of myObj at the time it was initially added to the database?
Thanks,