我有一個名爲TestMakeAValidCall()
的單元測試。它測試我的手機應用程序進行有效的通話。單元測試 - 是否有單元測試調用其他單元測試的不良形式
我即將編寫另一個測試,名爲TestShowCallMessage()
,需要有一個有效的測試呼叫。在該測試中打電話給TestMakeAValidCall()
是不是很糟糕?
供參考這是我的TestMakeAValidCall()
測試。
[TestMethod]
public void TestMakeAValidCall()
{
//Arrange
phone.InCall = false;
phone.CurrentNumber = "";
// Stub the call to the database
data.Expect(x => x.GetWhiteListData()).
Return(FillTestObjects.GetSingleEntryWhiteList());
// Get some bogus data
string phoneNumber = FillTestObjects.GetSingleEntryWhiteList().
First().PhoneNumber;
// Stub th call to MakeCall() so that it looks as if a call was made.
phone.Expect(x => x.MakeCall(phoneNumber)).
WhenCalled(invocation =>
{
phone.CurrentNumber = phoneNumber;
phone.InCall = true;
});
//Act
// Select the phone number
deviceControlForm.SelectedNumber = phoneNumber;
// Press the call button to make a call.
deviceMediator.CallButtonPressed();
//Assert
Assert.IsTrue(phone.InCall);
Assert.IsTrue(phone.CurrentNumber == phoneNumber);
}
感謝所有的好的答案。我將重複代碼重構爲單獨的調用。我以最多的選票選出答案。 – Vaccano 2009-09-02 17:17:19