0

we are using RhinoMocks to unit test our wpf app.

in one of the scenarios we have a method --

public void LetsAdd()
{
    _mockableInterface1.DoSomething();
    _mockableInterface2.DoSomethingElse();
}

Now when I try to unit test LetsAdd method, I can assert if both doSomething() and DoSomethingElse() were called or not. but can I somehow also test if they were executed in this very order?

so in other words can I write a test which fails if I swap the two method calls.. ?

Thanks

Muds
  • 4,006
  • 5
  • 31
  • 53
  • 3
    possible duplicate of [Can I test method call order with AAA syntax in Rhino-Mocks 3.6?](http://stackoverflow.com/questions/8653354/can-i-test-method-call-order-with-aaa-syntax-in-rhino-mocks-3-6) – Pierre-Luc Pineault Feb 23 '15 at 17:25
  • Curious why this is needed. If you have requirements that one method be called before another, shouldn't that be enforced in code rather than tests? – Rufus L Feb 23 '15 at 17:32
  • @Pierre-LucPineault... I guess I need to improve my search skills... thanks for the find bud. I will try this if it works – Muds Feb 23 '15 at 17:37
  • @RufusL - in most cases what you say holds true – Muds Feb 23 '15 at 17:37
  • 2
    @RufusL There's multiple scenarios where that could happen. For example he could be trying to save two different entities through his DAL, where one must be saved before the other. Inverting the calls could blow up only at the DB level (because of constraints, or whatever), which is an abstraction layer below what's he's testing. Since it's in the DB, the code can't reflect that (no return value, for example). Or a simple `.Validate()` then .`Process()`, where Validate does not return a value and you want to be sure it's called before the processing. Or loading resources in XNA. – Pierre-Luc Pineault Feb 23 '15 at 17:42

1 Answers1

0

If you have preconditions, the test must fire them prior to the test logic. Never rely on the order of the tests. If there is particular data that needs to be set up for a test, you should set it up and then clean it after you are finished. This can be done in any testing library through setup and clean up methods. If your classes are fine grained enough, you can use the class setup and clean up methods, otherwise you have to focus on the test setup and clean up methods.

You could take the method of testing if things were set up (both methods fired?), but this can end up nullifying your test because one test was run in the "wrong" order. But since each test should be autonomous, there is not such thing as "wrong" order.

In your particular scenario, you should set it up to run in the order you needed tested. If all scenarios are valid, you need multiple tests. Here is what I mean by all scenarios (DS = DoSomething and DSE = DoSomethingElse):

Neither DS or DSE run DS only DSE only Both, DS first Both, DSE first

If different results are expected, by business rules, then you need all 5 tests. Rather than test for the order of the routines, your goal should be to be explicit about testing, forcing them to run in order.

If this cannot be accomplished in your tests, then I see two options:

  1. Re-architecting the solution to remove the "randomness"
  2. Adding some type of tracing for method order that can be queried
Gregory A Beamer
  • 16,870
  • 3
  • 25
  • 32