Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you test a referenced class that performs internal operations?
    text
    copied!<p>How would I test the following code?</p> <pre><code> Public Sub SetSerialIdForDevice() Try Dim component As Object = container.getComponentRef("componentInterface") If component IsNot Nothing Then component.SetupDeviceSerialID(container.serialNumbers) Else serialfound = False End If Catch ex As Exception '' error handling End Try End Sub </code></pre> <p>Project references (or components as they're called here) are loaded at runtime into a singleton 'container.' We call the component that interfaces with a device by using the <code>container.getComponentRef("&lt; name of component we're looking for &gt;")</code>. We then invoke a method on this component to set the serial id, which is stored in a property of the container object. </p> <p>In the SetupDeviceSerialID() method we call properties native to the 'container' (such as if it's an internal debug mode) as well as some other objects. What would be the best way to test this scenario where we have objects within objects? Would we mock all the calls, properties, and objects in order to isolate the test for SetupDeviceSerialID()?</p> <p>Or do we mock the <code>'componentInterface'</code> that's returned and mock the call for <code>SetupDeviceSerialID()</code> and then test the properties that were changed within <code>SetupDeviceSerialID()</code>?</p> <p><strong>EDIT</strong></p> <p>I've been thinking about testing this incorrectly (obviously) and through the answers I've come to realize that I was trying to cram testing for methods executed deeper in the code, into tests for the SetSerialIdForDevice() method.</p> <p>So as a result, If a serialID is found, we would set <code>serialfound = true</code> inside of <em>SetupDeviceSerialID()</em>.<br> Is that something we would test here (since we will be testing for <code>serialfound=false</code>), or in a test for <em>SetupdDeviceSerialID()</em>? And would we create a test to see if <em>SetupDeviceSerialID()</em> actually exists on the "componentInterface" component?</p>
 

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