做者: 梅樺 發表於 2010-05-10 17:21 原文連接 閱讀: 97 評論: 0html
(一)屬性Property程序員
屬性也是一種方法。因此對於屬性的指望,和方法是同樣的。方法和屬性的指望在前幾篇隨筆中已經大量使用。網站
一般的讀或寫屬性器的指望spa
[Test]
public
void
TestEvent()
{
MockRepository mocks
=
new
MockRepository();
IList
<
int
>
_list
=
mocks.DynamicMock
<
IList
<
int
>>
();
Expect.Call(_list.Count).Return(
5
);
mocks.ReplayAll();
Assert.AreEqual(
5
, _list.Count);
}
這個是很簡單的。而後還有一種自動屬性指望的,設置屬性行爲來達到自動屬性指望安裝。這個有兩種方式,在前邊說Mock四種類型時說過:code
一是傳統的一個一個的安裝,還有一種方式就是經過Stub方式實現。orm
public
interface
ICustomer
{
int
Unid {
get
;
set
; }
string
CustomerName {
get
;
set
; }
string
Address {
get
;
set
; }
string
ShowTitle(
string
str);
}
這個接口有3個屬性。視頻
[Test]
public
void
TestProperty()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.DynamicMock
<
ICustomer
>
();
using
(mocks.Record())
{
Expect.Call(customer.Unid).PropertyBehavior();
Expect.Call(customer.CustomerName).PropertyBehavior();
Expect.Call(customer.Address).PropertyBehavior();
}
customer.Unid
=
5
;
Assert.AreEqual(
5
, customer.Unid);
}
經過這種方法要分別爲mock對象設置屬性行爲。而經過Stub則很簡單:htm
[Test]
public
void
TestPropertyStub()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.Stub
<
ICustomer
>
();
customer.Unid
=
5
;
Assert.AreEqual(
5
, customer.Unid);
}
經過PropertyBehavior()方法能夠爲屬性模擬行爲。這上行爲會在模擬對象的生命週期內有效。模擬對象(mock object)與模擬(mock)是兩個概念。 對象
(二)方法blog
屬性器有讀和寫屬性,在安裝指望時,只是對寫屬性設置便可。在方法中有多種狀況:
(1)無返回值
void
NoValues(
string
str);
安裝指望:
Expect.Call(
delegate
{ customer.NoValues(
""
); });
(2)帶返回值
這個前邊已經大量使用
string
ShowTitle(
string
str);
安裝指望:
Expect.Call(customer.ShowTitle(
""
)).Return(
"
爲空
"
);
(3)帶輸出參數或引用參數的方法
[Test]
public
void
TestOutPara()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.DynamicMock
<
ICustomer
>
();
string
strOut
=
"
123
"
;
Expect.Call(customer
.OutParas(
""
,
out
strOut))
.Return(
"
test
"
).OutRef(
"
xxx
"
);
mocks.ReplayAll();
customer.OutParas(
""
,
out
strOut);
Assert.AreEqual(
"
xxx
"
, strOut);
}
看粗體部分。帶輸出或引用參數的方法安裝指望和正常調用的方式類似。
這裏說明一下,RhinoMock3.5支持lambda和3.x擴展屬性,因此在安裝時能夠這樣:
customer.Expect(p
=>
p.OutParas(
""
,
out
strOut)).Return(
"
test
"
).OutRef(
"
xxx
"
);
這個要結合Action和Func來分析,尤爲是Action。
(三)方法選項
能夠對安裝指望的方法設置選項(options),例如:安裝完指望的方法的可調用次數。
設置方法是:Expect.Call或LastCall
這裏分別說一下:
(1)Return
固然最長見的就是返回值,爲讀屬性或帶返回值的方法返回值設置,與指望匹配。
[Test]
public
void
TestMethodOptions()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.DynamicMock
<
ICustomer
>
();
using
(mocks.Record())
{
Expect.Call(customer.Unid)
.Return(
10
);
}
Assert.AreEqual(
10
, customer.Unid);
}
(2)Throw
異常拋出。
[Test]
public
void
TestMethodOptions()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.DynamicMock
<
ICustomer
>
();
using
(mocks.Record())
{
Expect.Call(customer.ShowTitle(
""
))
.Throw(
new
Exception(
"
不能爲空
"
));
}
customer.ShowTitle(
""
);
}
結果:failed: System.Exception : 不能爲空
(3)方法容許使用的次數
[Test]
public
void
TestMethodOptions()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.DynamicMock
<
ICustomer
>
();
using
(mocks.Record())
{
Expect.Call(customer.ShowTitle(
""
))
.Return(
"
不能爲空
"
)
.Repeat.Once();
}
Assert.AreEqual(
"
不能爲空
"
,customer.ShowTitle(
""
));
Assert.AreEqual(
"
不能爲空
"
, customer.ShowTitle(
""
));
}
安裝指望時,容許調用1次。因此對於兩次斷言使用,會有異常出現。除了Once,還有Twice(),Any等
(4)忽略方法參數
[Test]
public
void
TestMethodOptions()
{
MockRepository mocks
=
new
MockRepository();
var customer
=
mocks.DynamicMock
<
ICustomer
>
();
using
(mocks.Record())
{
Expect.Call(customer.ShowTitle(
null
))
.Return(
"
不能爲空
"
)
.IgnoreArguments();
}
Assert.AreEqual(
"
不能爲空
"
, customer.ShowTitle(
""
));
}
請看粗體部分,忽略了字串參數。
(5)簡單的自動屬性
PropertyBehavior()已經說過,再也不贅述
評論: 0 查看評論 發表評論
程序員找工做,就在博客園
最新新聞:
· 電子商務網站之信任度(2010-10-09 17:02)
· 馬雲:管理的核心在於「抓住人性的本真」(2010-10-09 16:52)
· 另外一 Windows Phone Live 主頁截圖現身 Windows Phone 7 視頻(2010-10-09 16:38)
· 谷歌首名員工:公司成功歸結於運氣不錯(2010-10-09 16:32)
· 神奇小子Geohot帶着「limera1n」迴歸(2010-10-09 16:29)
編輯推薦:遠離.NET
網站導航:博客園首頁 我的主頁 新聞 閃存 小組 博問 社區 知識庫