Testing an ordered sequence of callbacks

239 views
Skip to first unread message

mathematically

unread,
Jan 16, 2011, 9:46:26 AM1/16/11
to NSubstitute
Hi,

I need to test that my class makes call backs and and that the correct
number of calls are made and with the correct parameters.
Additionally though, I need to check that the calls occurred in the
*correct* order.

I have managed to do this using When..Do and building up a list of the
calls made and checking that matches my expectations, but I can't help
feeling that I am replicating information NSubstitute is already
recording and probably exposes this somehow.

See example in https://gist.github.com/781836 (Ignore the Hamcrest
style assertions).

Thanks in advance and keep up the good work on NSubstitute.
Matthew

David Tchepak

unread,
Jan 16, 2011, 5:07:54 PM1/16/11
to nsubs...@googlegroups.com
Hi Matthew,

You can access the raw info NSub has recorded using the ReceivedCalls extension method (e.g. target.ReceivedCalls()). This returns an IEnumerable<ICall> that you can query. I'm not sure that this will work out any neater than what you have already; I've tended to use the same approach you have, or if possible tried to refactor to eliminate the timing dependency. 

I should also point out that the ReceivedCalls method and the ICall interface is subject to change (hence why this method is undocumented). 

Support for checking ordered calls and the number of calls are planned for future releases.

Hope this helps.

David



--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To post to this group, send email to nsubs...@googlegroups.com.
To unsubscribe from this group, send email to nsubstitute...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nsubstitute?hl=en.


Message has been deleted

Jeremy

unread,
Aug 23, 2012, 6:55:03 PM8/23/12
to nsubs...@googlegroups.com
Has this been improved yet?  Or will this be a 1.5+ feature release?

Currently I have to do something like this:

    [TestFixture]     public class ExecuteTaskTest     {         [Test]         public void ExecuteCallsAllMethodsInCorrectOrder()         {             var task = Substitute.For<ExecuteTask>();             task.When(t => t.TestRunTask()).Do(x => task.Received(1).TestInit());             task.When(t => t.TestCleanup()).Do(x => task.Received(1).TestRunTask());             task.Execute();             task.Received(1).TestCleanup();         }     }     internal abstract class ExecuteTask     {         public abstract void TestInit();         public abstract void TestCleanup();         public abstract void TestRunTask();

        public void Execute()         {             this.TestInit();             this.TestRunTask();             this.TestCleanup();         }      }

David Tchepak

unread,
Aug 24, 2012, 2:23:40 AM8/24/12
to nsubs...@googlegroups.com
Hi Jeremy,

Nothing improved yet. It doesn't come up very often as a problem when I'm developing, so I've kept pushing it back.
I'll have another look at it.

Regards,
David

--
You received this message because you are subscribed to the Google Groups "NSubstitute" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nsubstitute/-/CPz18MA1MF4J.
Reply all
Reply to author
Forward
0 new messages