views:

1010

answers:

3

I have a Data Access Object TransactionDao. When you call TransactionDao.Save(transaction) I would like for it to setting a transaction.IsSaved=true flag (this is a simplification the actual thing I'm trying to do is not quite so banal). So when mocking my TransactionDao with RhinoMocks how can I indicate that it should transform its input?

Ideally I would like to write something like this:

Expect.Call(delegate {dao.Save(transaction);}).Override(x => x.IsSaved=true);

Does anyone know how to do this?


Though I got a hint how to do it from the answer specified below the actual type signature is off, you have to do something like this: Because of what Mark Ingram posted, seems like the best answer, though nobody's explicitly said it, is to do this:

public delegate void FakeSave(Transaction t);
...
Expect.Call(delegate {dao.Save(t); }).Do( new FakeSave(delegate(Transaction t2) { t.IsSaved = true; }));
A: 

you should mock the transaction and make it return true fo IsSaved, if you can mock the transaction of course.

ITransaction transaction = _Mocker.dynamicMock<ITransaction>;
Expect.Call(transaction.IsSaved).IgnoreArguments.Return(true);
_mocker.ReplayAll();
dao.Save(transaction);
chrissie1
Like I said, this is a simplification of what I'm doing, there are other changes that take place in the parameter and other flags that could be set. I need to know how to work with a byreference parameter basically
George Mauer
+1  A: 

You can accomplish this using the Do callback:

Expect.Call(delegate {dao.Save(transaction);})
    .Do(x => x.IsSaved = true);
Judah Himango
Thank you, though the syntax is not exactly correct, its giving me a cannot convert lambda to delegate error, once I figure out the exact syntax I'll post it and mark your answer.
George Mauer
George, since the Expect.Call(...).Do answer was correct (only syntax error), I would appreciate a 1 up vote for my answer.Thanks friend.
Judah Himango
+3  A: 

Gorge,

The simplest solution, which I found, applied to your question is the following:

Expect.Call(() => dao.Save(transaction))
    .Do(new Action<Transaction>(x => x.IsSaved = true));

So you don't need to create a special delegate or anything else. Just use Action which is in standard .NET 3.5 libraries.

Hope this help. Frantisek

frantisek