Build an Interface


We want our MockTextBox to be able to plug into the PutText method just like a TextBox would. To do that, it has to respond reasonably to just three messages: Lines, SelectionStart, and ScrollToCaret. So first we ll define that interface and then we ll use it. For now, I m going to do that in the Notepad object, the superclass of our XMLNotepad. That s where the TextBox gets set up. The file looks like this, with the interface done:

 using System; 
using System.Drawing;
using System.Windows.Forms;
namespace Notepad {
interface ITestTextBox {
string[] Lines {get; set;}
int SelectionStart { get; set;}
void ScrollToCaret();
}
class TestableTextBox: TextBox, ITestTextBox {
}
class NotepadCloneNoMenu : Form
{
public TestableTextBox textbox;
public NotepadCloneNoMenu() {
Text = "Notepad Clone No Menu";
textbox = new TestableTextBox();
textbox.Parent = this;
textbox.Dock = DockStyle.Fill;
textbox.BorderStyle = BorderStyle.None;
textbox.Multiline = true;
textbox.ScrollBars = ScrollBars.Both;
textbox.AcceptsTab = true;
}
}

I m skipping over a small amount of pain here. Setting up the interface was a bit tricky because I didn t know the syntax for the properties (Lines and SelectionStart) and it took a while to find it. But the result is fairly simple: We define an interface ITestTextBox with the three methods. Then we create a new class TestableTextBox that inherits from TextBox and from ITestTextBox. This is the first time I ve done this, but what I m told is that this is the standard way of setting up for using a Mock Object. You define the methods that you want to mock in a separate interface, and then you have the object you plan to test on inherit from the original object (which already defines those methods) and from the interface. Since the object defines the methods , using the interface just allows you to refine access later.

And we ll do that now. First a build to be sure this all works...and it does. Now I should be able to change the parameters of PutText() to allow me to use my Mock Object in it:

 public void PutText(ITestTextBox textbox, string[] lines, int selectionStart) { 
textbox.Lines = lines;
textbox.SelectionStart = selectionStart;
textbox.ScrollToCaret(); // keep cursor on screen. no test.
}

The only change is that the textbox is now declared as an ITestTextBox. We just have to make our MockTextBox use that interface, and we should be golden. Here are the stages:

 public class MockTextBox: ITestTextBox 
{
public MockTextBox()
{
}
public Boolean Scrolled {
get {
return false;
}
}
}

This doesn t compile yet, because MockTextBox doesn t implement the methods of the interface. So I ll add Lines, SelectionStart, and ScrollToCaret, all trivially. (I am told that Visual Studio 2003 provides some automated help with this.) First SelectionStart:

 public int SelectionStart { 
get { return 1; }
set {}
}

That compiles, so I must be doing it right. I ll do the others:

 public string[] Lines { 
get { return new string[0]; }
set {}
}
public void ScrollToCaret() {
}

That should be enough to get the TestScroll to compile with all the comments off. It looks like this:

 [Test] public void ScrollHappens() { 
int selectionStart = 1;
string[] lines = new String[] { "hello", "world" };
MockTextBox mock = new MockTextBox();
XMLNotepad notepad = new XMLNotepad();
Assert("no scroll", !mock.Scrolled);
notepad.PutText(mock, lines, selectionStart);
Assert("scroll happens", mock.Scrolled);
}

It does compile, and the test fails, as expected, on the scroll happens call. The flag isn t set. Now to fix the MockTextBox to detect whether it s scrolled. I ll add a variable, set it to false, and return it. Inside the ScrollToCaret method, I ll set it to true. That should do the job and make the test run.

 public class MockTextBox: ITestTextBox 
{
private Boolean scrolled = false;
public MockTextBox()
{
}
public int SelectionStart {
get { return 1; }
set {}
}
public string[] Lines {
get { return new string[0]; }
set {}
}
public void ScrollToCaret() {
scrolled = true;
}
public Boolean Scrolled {
get {
return scrolled;
}
}
}

And the test runs! Let s think back and assess what has just happened .




Extreme Programming Adventures in C#
Javaв„ў EE 5 Tutorial, The (3rd Edition)
ISBN: 735619492
EAN: 2147483647
Year: 2006
Pages: 291

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net