0
我有一些代碼,如下所示,使用多個方法調用Web服務從數據庫獲取一些數據。這將產生一組字段,然後將這些字段從Web應用程序添加到另一個數據庫。這一切都很好,但我不知道如何對它進行單元測試,因爲它主要輸出空白,並且數據來自每次點擊按鈕時都會改變的數據庫。有沒有辦法單元測試,只是方法的工作與否?對不起,我對單元測試非常陌生,但我知道它有多重要,所以我將不勝感激。void類型的單元測試方法
//Get webservice service
private Service1 GetService()
{
return new TestProjectService.Service1();
}
//Choose which webservice we want to use based on radio button selection
private TestProjectService.CommandMessages GetCommand(Service1 service)
{
var command = new TestProjectService.CommandMessages();
switch (WebServiceRadio.SelectedIndex)
{
case 0:
command = service.GetData();
break;
case 1:
command = service.GetDataLINQ();
break;
}
return command;
}
//Display the results in a label on screen
private void DisplayResult(string text)
{
LatestCommandLabel.Text = text;
}
//Get the current username of the user logged in
public string GetUsername()
{
return System.Security.Principal.WindowsIdentity.GetCurrent().Name;
}
//Submit the data to the database using Linq
private void SubmitData(string username, TestProjectService.CommandMessages command)
{
var dc = new TestProjectLinqSQLDataContext();
var msg = new TestProjectCommandMessage
{
Command_Type = command.CommandType,
Command = command.Command,
DateTimeSent = command.DateTimeSent,
DateTimeCreated = command.DateTimeCreated,
Created_User = username,
Created_Dttm = DateTime.Now
};
dc.TestProjectCommandMessages.InsertOnSubmit(msg);
dc.SubmitChanges();
}
//Return the value and submit data to database
private void ReturnValue()
{
var service = GetService();
var command = GetCommand(service);
var username = GetUsername();
if (command != null)
{
DisplayResult(String.Format("Last Command Called (Using {0}) : {1}", WebServiceRadio.SelectedItem.ToString(), command.Command));
string userName = GetUsername();
SubmitData(username, command);
}
else
{
DisplayResult("No Commands Available");
}
}
//Onlick return value
protected void GetCommandButton_Click(object sender, EventArgs e)
{
ReturnValue();
}
我不確定是什麼問題。爲什麼您認爲您的方法必須返回任何內容才能檢查更改是否已保存? – Tarec
我是單元測試新手 - 我知道如何單元測試帶出字符串的方法。然而,如果有一個空白,我不確定我正在檢查的是什麼(如果這是有道理的?!)。 CommandMessages對象中的數據來自數據庫的事實也意味着我不確定數據是什麼,所以我不確定如何獲得預期的值。 – user3284707
你想單元測試什麼方法? – Andrei