C# basic testing

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hey there, Im studying a .net module atm, and we are doing unit testing. Im trying to get some advice on whats going on, so my first example is a working one and the 2nd and third examples are my questions:
<div style="color:Black;background-color:White; <pre>
<span style="color:Green; //this is my calculator class
<span style="color:Blue; public <span style="color:Blue; class calculator
{
<span style="color:Blue; public <span style="color:Blue; int Add(<span style="color:Blue; int n1, <span style="color:Blue; int n2)
{
<span style="color:Blue; return n1 + n2;
}
<span style="color:Blue; public <span style="color:Blue; int Subtract(<span style="color:Blue; int n1, <span style="color:Blue; int n2)
{
<span style="color:Blue; return n1 - n2;
}
<span style="color:Blue; public <span style="color:Blue; int Multiply(<span style="color:Blue; int n1, <span style="color:Blue; int n2)
{
<span style="color:Blue; return n1 * n2;
}
<span style="color:Blue; public <span style="color:Blue; int Divide(<span style="color:Blue; int n1, <span style="color:Blue; int n2)
{
<span style="color:Blue; return n1 / n2;
}
}




<span style="color:Green; //so this is a seperate class to testing the add method in my //calculator class...
<span style="color:Blue; using ...
<span style="color:Blue; using NUnit.Framework;

<span style="color:Blue; namespace CalcLib
{
[TestFixture]
<span style="color:Blue; class CalculatorTest
{
[Test]
<span style="color:Blue; public <span style="color:Blue; void Test_Add()
{
calculator calc = <span style="color:Blue; new calculator();
<span style="color:Blue; int answer = calc.Add(4, 6);
Assert.AreEqual(10, answer);
}

}
}


[/code]
<br/>
<br/>

The following methods are available for the Box class:<br/>
Box (int x, int y, int height, int width) //constructor<br/>
getX () //returns the x coordinate of the lower left corner of the Box object<br/>
getY () //returns the y coordinate of the lower left corner of the Box object<br/>
getHeight () //returns the height of the Box object in pixels<br/>
get Width () //returns the width of the Box object in pixels
<br/>
The following methods are available for the Point class:<br/>
Point (int x, int y) //constructor<br/>
getX () //returns the x coordinate of the Point object<br/>
getY () //returns the y coordinate of the Point object<br/>
The lower left corner of the screen has the coordinates (0, 0).
<br/>
A new method is required for the fictional ClickChecker class which can be used to check if a person has clicked the mouse (generating an instance of the Point class) within a rectangular box positioned on screen (represented by an instance of the Box class).<br/>
Demonstrate, by writing the C# code needed and by outlining the steps that could be taken, how the Test Driven Development cycle may be applied when carrying out this task.
So im trying:

<div style="color:Black;background-color:White; <pre>
Using...
<span style="color:Blue; using NUnit.Framework;

<span style="color:Blue; namespace Boxes
{
[Text Fixture]
<span style="color:Blue; class ClickChecker
{
[Test}
<span style="color:Blue; public <span style="color:Blue; void mouseClickes()
{
Point p = <span style="color:Blue; new Point(x,y);
mouseposition aswer = p.Point(x,y);
Assert.AreEqual(p,answer)
}
}
[/code]
<br/>
Keeping my first example in mind could someone please point me out what Im doing wrong and possibly how to amend it, I know I have to get the new point and compare it to the current.

Another example is something like:
The following task is taken from a fictional Sprint Backlog.<br/>
Add a new method to the (fictional) StringValidator class to check if a string meets the following criteria:<br/>
o<br/>
Contains at least one digit<br/>
o<br/>
Contains at least one letter<br/>
o<br/>
Contains no spaces<br/>
o<br/>
Is at least 9 characters long<br/>
Demonstrate, by showing the C# code written and by outlining the steps taken, how the Test Driven Development cycle should be applied when carrying out this task.

Any suggestions would be greatly appreciated.

Thanks again guys


View the full article
 
Back
Top