Easieas way to ensure UpdateDatabase call

Skip to first unread message

Dmitriy Nagirnyak

Aug 5, 2008, 3:38:48 AM8/5/08

What is the easies way to ensure UpdateDatabase is called in unit tests?
Better would be to know the objects that are going to be updated.

100% working solution is implementing IPersistenceService and chain the calls
to the original one.
Are there any ideas to implement it with ~<50 lines of code plus <=3 lines
per test?

The aim is 2-3 lines of code per test to ensure object is going to be persisted.
It should not break the IPersistenceService itself (so mocking is tricky).

.NET 3.5

I can buy some beer for an elegant solution or can promise discount at http://www.argocomputing.com.au/Services/DCPortal.aspx

I expect to write the code like this:
public void Save_PersistsObject() {
.With(new { Username="test"} );
// Should save User with Username
Controller.Save(new UserData {
Username= "test"



Peter Morris

Aug 5, 2008, 8:25:20 AM8/5/08
Check there are no dirty objects?

Dmitriy Nagirnyak

Aug 5, 2008, 9:22:27 AM8/5/08
Hello Peter,

> Check there are no dirty objects?

<=3 Lines of code per test.

Absence of dirty objects doesn't guaranty objects were persisted. They could
just never beeing created.

This what I've came up for now:
public void Save_PersistsDatabase() {
string name = Unique;
Controller.Save(new PersonController.EditData {
FirstName = name
var person = WorkSpace.Query<Person>().Where(p=>p.FirstName == name).Single();

Query is extension method over IEcoServiceProvider that just return IQuaryable.
IsFullyPersisted is extension method over ILoopBack that return true if object
is NOT (New | Dirty | Deleted).

Disadvantage here - using IQuryable which is not performant for ECO (might
run lots of SQL, XPath or just in memory).
But while I don't run tests agains DB I don't care about it. For now.

Looking at this code I've just realised tha it can be simplified:

public void Save_PersistsObject() {
var newId = Controller.Save(new PersonController.EditData {
FirstName = Unique
var person = WorkSpace.GetObject<Person>(newId);
Assert.IsTrue(person.IsFullyPersisted(), "Not saved.");

It only relies on IsFullePersisted (StateService.IsNew,IsDirty, IObject.Deleted).


It is not what I wanted but works for now very good.
It seems my initial idea were to record what should happen with ECO objects
and then verify it.
It'd involve stages like:
1. Record actions and conditions.
2. Save current ECO state.
3. Exercise.
4. Save current ECO state.
5. Compare 2 and 4 and veryfy conditions.

Looks like common implementation of Mocking Frameworks.
Not sure I really need it.

Peter Morris

Aug 6, 2008, 6:35:23 AM8/6/08
What I do is to always use the IEcoServiceProvider to obtain all service
references. The I use Rhino Mocks to register a mock IPersistenceService
and add an Expect.Call for UpdateDatabaseWithList


Dmitriy Nagirnyak

Aug 6, 2008, 8:13:36 AM8/6/08
Hello Peter,

> Expect.Call for UpdateDatabaseWithList

How do you know what exactly has been saved?

Peter Morris

Aug 7, 2008, 6:27:47 AM8/7/08
If you need to do that I would instead write a passthough class which has an
INT property on it and throws an exception if the number of objects being
saved is incorrect.


Reply all
Reply to author
0 new messages