2010年6月23日 星期三

[.NET]單元測試練習

為了練習測試驅動開發,今天特別寫了一點小程式來做練習範例
參考資料

[TestMethod]
public void TestID()
{
//
// TODO: Add test logic here
//
Orz orz = new Orz();
orz.ID = 10;
Assert.AreEqual(10, orz.ID, "Orz.ID property has error, please check it.");
}

[TestMethod]
public void TestValue()
{
//
// TODO: Add test logic here
//
Orz orz = new Orz();
orz.Value = 1001;
Assert.AreEqual(1001, orz.Value, "Orz.Value property has error, please check it.");
}

[TestMethod]
public void TestName()
{
//
// TODO: Add test logic here
//
Orz orz = new Orz();
orz.Name = "XD";
Assert.AreEqual("XD", orz.Name, "Orz.Name property has error, please check it.");
}
[TestMethod]
public void TestOrzMap()
{
//
//
TODO: Add test logic here
//
OrzMap map = new OrzMap();
Orz orz = new Orz();
orz.Name = "XD";
// this testing tell us the Map property was not initial.
map.Map.Add(orz.Name, orz);

Orz getOrz = (Orz)map.Map[orz.Name];

Assert.AreEqual(getOrz.Name, orz.Name);
}
[TestMethod]
public void TestErrorSample()
{
//
// TODO: Add test logic here
//
Orz orz = new Orz();
orz.ErrorSample = 12345;
Assert.AreEqual(12345, orz.ErrorSample, "Orz.ErrorSample property has error, please check it.");
}

[TestMethod]
public void TestRightFunc()
{
//
// TODO: Add test logic here
//
Orz orz = new Orz();
orz.RightFunc(1234, 5678);
Assert.AreEqual(1234, orz.ID, "Orz.RightFunc() has error, please check it.");
Assert.AreEqual(5678, orz.Value, "Orz.RightFunc() has error, please check it.");
}
[TestMethod]
public void TestErrorFunc()
{
//
// TODO: Add test logic here
//
Orz orz = new Orz();
orz.ErrorFunc(1234, 5678);
Assert.AreEqual(1234, orz.ID, "Orz.ErrorFunc() has error, please check it.");
Assert.AreEqual(5678, orz.Value, "Orz.ErrorFunc() has error, please check it.");
}

class OrzMap
{
Hashtable m_HashTable;

public OrzMap()
{
// mark this initial code to test it.
//m_HashTable = new Hashtable();
}
public Hashtable Map
{
get { return m_HashTable; }
set { }
}
}

class Orz
{
int m_i4ID;
int m_i4Value;
string m_sName;
int m_i4ErrorSample;

public Orz()
{
m_i4ID = 0;
m_i4Value = 0;
m_i4ErrorSample = 0;
m_sName = "";
}

public int ID
{
get { return m_i4ID; }
set { m_i4ID = value; }
}

public int Value
{
get { return m_i4Value; }
set { m_i4Value = value; }
}

public string Name
{
get { return m_sName; }
set { m_sName = value; }
}

public int ErrorSample
{
get { return m_i4ID; }
set { m_i4ErrorSample = value; }
}

public void RightFunc(int id,int value)
{
this.ID = id;
this.Value = value;
}

public void ErrorFunc(int id, int value)
{
this.Value = id;
this.ID = value;
}
}

沒有留言:

張貼留言

嘎嘎嘎