求C#的测试类(急用)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
enum Sexkind { Male, Female }
class Human
{
private string no;
public string No
{
get { return no; }
set
{
int y = int.Parse(no.Substring(0, 4));
if (no == null || no.Length != 12)
throw new Exception("长度不到十二位");
else if
(y > DateTime.Now.Year || y < DateTime.Now.Year - 100)
throw new Exception("年份错误");
else
no = value;
}
}
public string Name { set; get; }
public Sexkind Sex { set; get; }
public int count { set; get; }
public int Year
{
get { return int.Parse(no.Substring(0, 4)); }
}
public string Info
{
get { return No + "\t" + Name + "\t" + Sex; }
}
public Human()
{
count++;
}
public Human(string no, string name, Sexkind sex)
{
count++;
No = no;
Name = name;
Sex = sex;
}
public void Show()
{
Console.WriteLine(Info);
}
public void SayHello()
{
Console.WriteLine("Hello, human!");
}
public delegate void State(object sender, string message);
public event State OnIdle;
public event State OnWork;
static void Main(string[] args)
{
Human hu = new Human("201902211122", "哇哈哈", Sexkind.Male);
hu.Show();
hu.SayHello();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Q756552
{
class Human
{
private string no;
public string No
{
get { return no; }
set
{
no = value;
int y = int.Parse(no.Substring(0, 4));
if (no == null || no.Length != 12)
throw new Exception("长度不到十二位");
else if
(y > DateTime.Now.Year || y < DateTime.Now.Year - 100)
throw new Exception("年份错误");
}
}
public string Name { set; get; }
public Sexkind Sex { set; get; }
public static int count { set; get; }
public int Year
{
get { return int.Parse(no.Substring(0, 4)); }
}
public string Info
{
get { return No + "\t" + Name + "\t" + Sex; }
}
public Human()
{
count++;
}
public Human(string no, string name, Sexkind sex)
{
count++;
No = no;
Name = name;
Sex = sex;
}
public void Show()
{
Console.WriteLine(Info);
}
public void SayHello()
{
Console.WriteLine("Hello, human!");
}
public delegate void State(object sender, string message);
public event State OnIdle;
public event State OnWork;
public void DoWork()
{
if (OnWork != null)
OnWork(this, "I am working");
}
}
enum Sexkind { Male, Female }
class Program
{
static void Main(string[] args)
{
Human hu = new Human("201902211122", "哇哈哈", Sexkind.Male);
hu.SayHello();
hu.Show();
hu.OnWork += new Human.State(hu_OnWork);
hu.DoWork();
Console.WriteLine(hu.Info);
Human hu1 = new Human();
Console.WriteLine(Human.count);
}
static void hu_OnWork(object sender, string message)
{
Console.WriteLine("the work says: " + message);
}
}
}
使用nunit,xunit或mstest去做单云测试,如果需要模拟对象可以使用moq或nsubstitute mock framework去模拟对象