Note that there are some explanatory texts on larger screens.

plurals
  1. POUnit testing: how to test methods with a lot of underlying objects and business logic
    primarykey
    data
    text
    <p>I am really new to unit testing, and I can't figure out the proper way to do it for my case, though I spent crazy amount of time on research.</p> <p>My code base is huge (~3 years of work), very coupled unfortunately, hard to test and no unit testing has ever been done on it.</p> <p>So for instance, when trying to test a collection class <code>ProductCollection</code>, more specifically, the <code>bool MoveElementAtIndex(Product productToMove, int newIndex)</code> of it, I am encountering the following problems:</p> <ul> <li>first I have to initialize this <code>new ProductCollection()</code></li> <li>the constructor initializes another hand made class: <code>new KeyedList&lt;ID, Product&gt;</code>. I suppose this should not be called within this constructor, since I'm not testing <code>KeyedList</code>.</li> <li>next, I am trying to add 3 products to this <code>ProductCollection</code>.</li> <li>I am then first creating these 3 <code>new Product()</code>.</li> <li>But the constructor of <code>Product</code> class does several things</li> <li>It computes a unique ID for the newly created product: <code>this.ID = IDUtils.ComputeNewIDBasedOnTheMoonPhase()</code>. I suppose I should not test this neither, since it's not my scope. How should I avoid such calls at this level of deepness?</li> <li>The same Product constructor assigns some default properties for this product: <code>this.Properties = new ProductProperties(folderPathToDefaultProperties)</code>. This should not be called from my simple <code>FieldCollection.MoveElementAtIndex</code> test, right?</li> <li>Let's say I finally have my product objects now, and I'm trying to add them to my collection.</li> <li>But the <code>ProductCollection.Add(MyProduct)</code> checks if the underlying <code>KeyedList</code> already contains the product. This is also business logic that I should avoid, not being related to my test. The question is how?</li> <li>Also, in this <code>Add</code> method, some events are raised, notifying the system about several things (e.g., that a new product was added to the collection). These should also not be fired at all I suppose.</li> <li>And then finally, when I have my products added, I'm calling the desired SUT: the move elements method.</li> <li>But this method also has logic that can be out of scope for my test: it verifies that the underlying <code>KeyedList</code> actually contains those fields, it calls <code>KeyedList.Remove()</code>, <code>KeyedList.Insert()</code> for its moving logic, and it fires events like <code>CollectionModified</code>.</li> </ul> <p>If you could just explain me how to do this unit test properly, how to avoid underlying objects from being called, I would really appreciate it.</p> <p>I am thinking of Microsoft's Moles framework (VS2010), since I have the impression that it does not require me to refactor everything, as this is absolutely not an option. But having tried it already, still can't find a proper way to use it.</p> <p>Also, I have the impression that this concrete example will help many in my situation, because this is how code in real world usually is.</p> <p>Any thoughts?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload