测试超时
  [Test,Timeout(10*1000)]
  public void Test_WriteAndGetOneData()
  {
  ...
  }
  测试用例分类
  [Test,Timeout(10*1000)]
  [Category("FunctionTest")]
  public void Test_WriteAndGetMultipleData()
  {
  ....
  }
  单元测试中的RhinoMocks
  创建桩对象
  桩对象不参与验证过程,其作用是使代码可以顺畅地运行到完成或运行到你希望测试的点。
  例子
  [Test]
  public void CreateStub_1()
  {
  MockRepository mocks = new MockRepository();
  IAnimal animal = mocks.Stub<IAnimal>();
  animal.Legs = 1;
  Assert.AreEqual(1, animal.Legs);
  }
  创建模拟对象
  模拟对象参与验证过程,其作用是为了检测对应的依赖对象是否被调用,以及调用的顺序是否正确等等
  严格:
  给模拟对象设定的期望必须被执行,没有设定的期望不能发生,否则调用Verify或VerifyAll时,会导致测试用例失败
  例子
[Test]
public void CreateStrictMock_1()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.StrictMock<ICustomerService>();
customerService.Expect(p => p.BuyBook(null));
mocks.ReplayAll();
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateStrictMock_2()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null)).Repeat.Any();
}
customerService.BuyBook(null);
customerService.BuyBook(null);
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateStrictMock_3()
{
ICustomerService customerService = MockRepository.GenerateStrictMock<ICustomerService>();
customerService.Expect(p => p.BuyBook(null));
customerService.Expect(p => p.BuyTicket(null));
customerService.BuyBook(null);
customerService.BuyTicket(null);
customerService.VerifyAllExpectations();
}
[Test]
public void CreateStrictMock_ShouldBeFail()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.StrictMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null)).Repeat.Any();
}
customerService.BuyBook(null);
customerService.BuyTicket(null);//BuyTicket is not recorded and it cannot be called.
mocks.VerifyAll();
}
  非严格:
  给模拟对象设定的期望必须被执行,没有设定的期望可以发生,否则调用Verify或VerifyAll时,会导致测试用例失败
  例子
[Test]
public void CreateDynamicMock_1()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
customerService.Expect(p => p.BuyBook(null));
mocks.ReplayAll();
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateDynamicMock_2()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null));
}
customerService.BuyBook(null);
mocks.VerifyAll();
}
[Test]
public void CreateDynamicMock_3()
{
MockRepository mocks = new MockRepository();
ICustomerService customerService = mocks.DynamicMock<ICustomerService>();
using (mocks.Record())
{
customerService.Expect(p => p.BuyBook(null)).Repeat.Any();
}
customerService.BuyBook(null);
customerService.BuyTicket(null);//Even though BuyTicket is not recorded and it be called.
mocks.VerifyAll();
}
[Test]
public void CreateDynamicMock_4()
{
MockRepository mocks = new MockRepository();
IAnimal animal = mocks.DynamicMock<IAnimal>();
Expect.Call(animal.Legs).PropertyBehavior();
animal.Legs = 1;
Assert.AreEqual(1, animal.Legs);
}
  创建部分模拟对象
  部分模拟对象可以用于测试抽象类中的模板方法。
  例子
[Test]
public void CreatePartialMock_1()
{
MockRepository mocks = new MockRepository();
AbstractProcessor processor = mocks.PartialMock<AbstractProcessor>();
using (mocks.Record())
{
Expect.Call(processor.GetStatus()).Return(0);
Expect.Call(processor.GetStatus()).Return(1);
}
Assert.AreEqual("OK", processor.Process());
Assert.AreEqual("NOTOK", processor.Process());
mocks.VerifyAll();
}
public abstract class AbstractProcessor
{
public string Process()
{
if (GetStatus() == 0)
{
return "OK";
}
else
{
return "NOTOK";
}
}
public abstract int GetStatus();
}
  模拟对象属性调用
[Test]
public void Property_1()
{
MockRepository mocks = new MockRepository();
Ticket ticket = mocks.StrictMock<Ticket>();
using (mocks.Record())
{
Expect.Call(ticket.Movie).PropertyBehavior();
Expect.Call(ticket.Price).PropertyBehavior();
}
ticket.Movie = "ABC";
ticket.Price = 10.0f;
Assert.AreEqual("ABC", ticket.Movie);
Assert.AreEqual(10.0f, ticket.Price);
ticket.VerifyAllExpectations();
}
[Test]
public void Property_2()
{
MockRepository mocks = new MockRepository();
Ticket ticket = mocks.StrictMock<Ticket>();
using (mocks.Record())
{
Expect.Call(ticket.Movie).Return("ABC");
Expect.Call(ticket.Price).Return(10.0f);
}
Assert.AreEqual("ABC", ticket.Movie);
Assert.AreEqual(10.0f, ticket.Price);
ticket.VerifyAllExpectations();
}
[Test]
public void Property_3()
{
MockRepository mocks = new MockRepository();
Book book = mocks.Stub<Book>();
book.Name = "ABC";
book.Price = 10.0f;
Assert.AreEqual("ABC", book.Name);
Assert.AreEqual(10.0f, book.Price);
}
  模拟delegate和设定模拟对象方法被调用的顺序
[Test]
public void Ordered_OK()
{
MockRepository mocks = new MockRepository();
ICustomerService customer = mocks.StrictMock<ICustomerService>();
using (mocks.Ordered())
{
customer.BuyTicket(null);
customer.BuyBook(null);
}
customer.Replay();
customer.BuyTicket(null);
customer.BuyBook(null);
customer.VerifyAllExpectations();
}
[Test]
public void Ordered_ShouldFail()
{
MockRepository mocks = new MockRepository();
ICustomerService customer = mocks.StrictMock<ICustomerService>();
using (mocks.Ordered())
{
customer.BuyTicket(null);
customer.BuyBook(null);
}
customer.Replay();
customer.BuyBook(null);
customer.BuyTicket(null);
customer.VerifyAllExpectations();
}
[Test]
public void Delegate_1()
{
MockRepository mocks = new MockRepository();
var oo = mocks.DynamicMock<DoThing>();
oo("BuyBook");
oo.Replay();
oo("BuyBook");
oo.VerifyAllExpectations();
}
[Test]
public void Delegate_Action()
{
MockRepository mocks = new MockRepository();
var oo = mocks.DynamicMock<Action<string>>();
oo("BuyBook");
oo.Replay();
oo("BuyBook");
oo.VerifyAllExpectations();
}
[Test]
public void Delegate_Func()
{
MockRepository mocks = new MockRepository();
var oo = mocks.DynamicMock<Func<string,string>>();
Expect.Call(oo("BuyBook")).Return("OK");
oo.Replay();
Assert.AreEqual("OK", oo("BuyBook"));
oo.VerifyAllExpectations();
}
public delegate void DoThing(string thing);
}