Note that there are some explanatory texts on larger screens.

plurals
  1. PORecording interaction on an inflection point using mocking framework. Moq
    primarykey
    data
    text
    <p><strong>Updated version of question</strong></p> <p>Hi. </p> <p>My company has a few legacy code bases which I hope to get under test as soon as they migrate to .NET 3.5. I have selected Moq as my Mocking framework (I fell in love with the crisp syntax immediately). </p> <p>One common scenario, which I expect to see a lot of in the future, is where I see an object which interacts with some other objects. </p> <p>I know the works of Michael Feathers and I am getting good at identifying inflection points and isolating decent sized components. Extract and Override is king.</p> <p>However, there is one feature which would make my life a whole lot easier.</p> <p>Imagine Component1 interacting with Component2. Component2 is some weird serial line interface to a fire central or somesuch with a lot of byte inspection, casting and pointer manipulation. I do not wish to understand component2 and its legacy interface consumed by Component1 carries with it a lot of baggage. </p> <p>What I would like to do, is to extract the interface of Component2 consumed by Component1 and then do something like this:</p> <pre><code> component1.FireCentral = new Mock&lt;IComponent2&gt; (component2); </code></pre> <p>I am creating a normal mock, but I am pasing in an instance of the real Component2 in as a constructor argument into the Mock object. It may seem like I'm making my test depending on Component2, but I am not planning on keeping this code. This is part of the "place object under test" ritual.</p> <p>Now, I would fire up the real system (with a physical fire central connected) and then interact with my object.</p> <p>What I then would wish for, is to inspect the mock to see a log of how component1 interacted with component2 (using the debugger to inspect some collection of strings on the mock). And, even better, the mock could provide a list of expectations (in C#) that would create this behavior in a mock that did not depend on Component2, which I would then use in my test code.</p> <p>In short. Using the mocking framework to record the interaction so that I can play it back in my test code.</p> <p><strong>Old version of question</strong></p> <p>Hi.</p> <p>Working with legacy code and with a lot of utility classes, I sometimes find myself wondering how a particular class is acted upon by its surroundings in a number of scenarios. One case that I was working on this morning involved subclassing a regular MemoryStream so that it would dump its contents to file when reaching a certain size.</p> <pre><code>// TODO: Remove private class MyLimitedMemoryStream : MemoryStream { public override void Write(byte[] buffer, int offset, int count) { if (GetBuffer().Length &gt; 10000000) { System.IO.FileStream file = new FileStream("c:\\foobar.html",FileMode.Create); var internalbuffer = GetBuffer(); file.Write(internalbuffer,0,internalbuffer.Length); } base.Write(buffer, offset, count); } } </code></pre> <p>(and I used a breakpoint in here to exit the program after the file was written). This worked, and I found which webform (web part->web part->web part) control rendered incorrectly. However, memorystream has a bunch of write's and writeline's. </p> <p>Can I use a mocking framework to quickly get an overview on how a particular instance is acted upon? Any clever tricks there? We use Rhino Mocks.</p> <p>I see this as a great assett in working with legacy code. Especially if recorded actions in a scenario easily can be set up as new expectations/acceptance criteria for that same scenario replicated in a unit test.</p> <p>Every input is appreciated. Thank you for reading.</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.
    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