(一)MockRepository
RhinoMock的主要的类,是Mock仓库,用于创建mock对象,录制,重放,验证等。
(1)创建Mock
MockRepository mock = new MockRepository();
(2)创建mock对象
Mock的目的就是创建mock对象,然后期望,录制重放等。创建mock对象有很多方法,以前的版本中通过:
MockRepository fac = new MockRepository();
var customer = fac.CreateMock<ICustomer>();
var customer = fac.CreateMock<ICustomer>();
来进行,CreateMock方法已经过时,工厂方法来进行:
MockRepository fac = new MockRepository();
var customer = fac.StrictMock<ICustomer>();
var customer = fac.StrictMock<ICustomer>();
也可以通过静态工厂方法来创建:
var customer = MockRepository.GenerateMock<ICustomer>();
(3)录制
所有的mock框架都使用Record/Replay模式,但有些是显示的,有些是隐式的,而RhinoMock就是显示的。
·Record/Replay传统的录制与重放:
[Test]
public void TestRR()
{
MockRepository fac = new MockRepository();
var customer = fac.StrictMock<ICustomer>();
customer.Expect(p => p.ShowTitle("")).Return("");
customer.Replay();
Assert.AreEqual("", customer.ShowTitle(""));
}
public void TestRR()
{
MockRepository fac = new MockRepository();
var customer = fac.StrictMock<ICustomer>();
customer.Expect(p => p.ShowTitle("")).Return("");
customer.Replay();
Assert.AreEqual("", customer.ShowTitle(""));
}
·Record,Using方式
public void TestRR()
{
MockRepository fac = new MockRepository();
var customer = fac.StrictMock<ICustomer>();
using (fac.Record())
{
customer.Expect(p => p.ShowTitle("")).Return("");
}
Assert.AreEqual("", customer.ShowTitle(""));
}
{
MockRepository fac = new MockRepository();
var customer = fac.StrictMock<ICustomer>();
using (fac.Record())
{
customer.Expect(p => p.ShowTitle("")).Return("");
}
Assert.AreEqual("", customer.ShowTitle(""));
}
·通过静态工厂方法创建mock对象:
public void TestRR()
{
var customer = MockRepository.GenerateMock<ICustomer>();
customer.Expect(p => p.ShowTitle("")).Return("");
Assert.AreEqual("", customer.ShowTitle(""));
}
{
var customer = MockRepository.GenerateMock<ICustomer>();
customer.Expect(p => p.ShowTitle("")).Return("");
Assert.AreEqual("", customer.ShowTitle(""));
}
(4)验证
用于验证mock对象的期望是否成立。
·Verify,用于验证mock对象的所有期望是否满足
·VerifyAll,用于验证所有mock对象的所有期望是否满足
(二)Expect类
为有返回值的类的方法设置期望
Call:为方法或属性提供期望
·Call<T>(T ignored)
·Call(Action actionToExecute)
例:
[Test]
public void TestCall()
{
MockRepository mock = new MockRepository();
var mockObject = mock.StrictMock<ICustomer>();
Expect.Call(mockObject.ShowTitle(string.Empty)).Return("不能为空");
Expect.Call(mockObject.Unid).Return(30);
mock.ReplayAll();
Assert.AreEqual("不能为空", mockObject.ShowTitle(""));
Assert.AreEqual(30, mockObject.Unid);
}
public void TestCall()
{
MockRepository mock = new MockRepository();
var mockObject = mock.StrictMock<ICustomer>();
Expect.Call(mockObject.ShowTitle(string.Empty)).Return("不能为空");
Expect.Call(mockObject.Unid).Return(30);
mock.ReplayAll();
Assert.AreEqual("不能为空", mockObject.ShowTitle(""));
Assert.AreEqual(30, mockObject.Unid);
}
看lambda表达式方式
[Test]
public void TestCall()
{
MockRepository mock = new MockRepository();
var mockObject = mock.StrictMock<ICustomer>();
mockObject.Expect(p => p.ShowTitle("")).Return("不能为空");
mockObject.Expect(p => p.Unid).Return(30);
mock.ReplayAll();
Assert.AreEqual("不能为空", mockObject.ShowTitle(""));
Assert.AreEqual(30, mockObject.Unid);
}
public void TestCall()
{
MockRepository mock = new MockRepository();
var mockObject = mock.StrictMock<ICustomer>();
mockObject.Expect(p => p.ShowTitle("")).Return("不能为空");
mockObject.Expect(p => p.Unid).Return(30);
mock.ReplayAll();
Assert.AreEqual("不能为空", mockObject.ShowTitle(""));
Assert.AreEqual(30, mockObject.Unid);
}
再来看Using方式
[Test]
public void TestCallUsing()
{
MockRepository mock = new MockRepository();
var mockObject = mock.StrictMock<ICustomer>();
using (mock.Record())
{
mockObject.Expect(p => p.ShowTitle("")).Return("不能为空");
mockObject.Expect(p => p.Unid).Return(30);
}
Assert.AreEqual("不能为空", mockObject.ShowTitle(""));
Assert.AreEqual(30, mockObject.Unid);
}
public void TestCallUsing()
{
MockRepository mock = new MockRepository();
var mockObject = mock.StrictMock<ICustomer>();
using (mock.Record())
{
mockObject.Expect(p => p.ShowTitle("")).Return("不能为空");
mockObject.Expect(p => p.Unid).Return(30);
}
Assert.AreEqual("不能为空", mockObject.ShowTitle(""));
Assert.AreEqual(30, mockObject.Unid);
}