I am... kind of a beginner on unit testing.
I just read some Unit Testing Best Practices. Kind of understand the unit test is meant to prevent changes made on code that will break the application. And we will want create test cases on all the object's public API(getter, methods) to test the object's behavior to see if is as we are expected.
So now.. I have a little simple class I need to test:
public class Foo{
private readonly string _text;
public Foo(string initialText)
{
this._text = initialText;
}
public string Text {get;}
//Some other methods that will use this Text property to parsing, comparasion etc
public string RichTextFormat {....}
}
In here, this as in the comment, this Text property is use a lot of place for parsing, comparasion etc.
So I think is very important to ensure the Text property returns exactly what I passed inside the constructor.
Here is the test cases I wrote...
[TestMethod]
public void Text_WhenInitialTextIsNull()
{
string initalizeText = null;
Foo realFoo = new Foo(initalizeText);
Assert.AreEqual(initalizeText, realFoo.Text);
}
[TestMethod]
public void Text_WhenInitialTextIsEmpty()
{
string initalizeText = string.Empty;
Foo realFoo = new Foo(initalizeText);
Assert.AreEqual(initalizeText, realFoo.Text);
}
[TestMethod]
public void Text_WhenInitialTextIsOneLetter()
{
string initalizeText = "A";
Foo realFoo = new Foo(initalizeText);
Assert.AreEqual(initalizeText, realFoo.Text);
}
[TestMethod]
public void Text_WhenInitialTextIsOneSpecialCharacter()
{
string initalizeText = "!";
Foo realFoo = new Foo(initalizeText);
Assert.AreEqual(initalizeText, realFoo.Text);
}
[TestMethod]
public void Text_WhenInitialTextIsOneSentense()
{
string initalizeText = "Hello, World!";
Foo realFoo = new Foo(initalizeText);
Assert.AreEqual(initalizeText, realFoo.Text);
}
[TestMethod]
public void Text_WhenInitialTextIsOneParagraph()
{
string initalizeText = "Who's the Smartphone Winner? " + System.Environment.NewLine +
" On the smartphone front, however, iSuppli put Apple at number one," +
" while Strategy Analytics pointed to Samsung. " + System.Environment.NewLine +
" According to iSuppli, Apple shipped 35 million smartphones in the first quarter" +
" to Samsung's 32 million. Strategy Analytics, however, said Samsung's total was" +
" 44.5 million to Apple's 35.1 million. Nokia landed at number three on both lists" +
" with 12 percent market share. ";
Foo realFoo = new Foo(initalizeText);
Assert.AreEqual(initalizeText, realFoo.Text);
}
I wonder if this... is too heavy?