??????????
????[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??????2??????????
????????
[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??????2??????????
????????
[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);
}