嘗試訪問實體框架的單元測試函數。所以我試圖把所有的實體代碼放到下面的測試函數中?然而它停止在Linq聲明中;顯然試圖訪問數據庫對它來說太過戲劇化了。也許一個解決方法是在基於sql lite或compact的單元測試函數中創建一個副本數據庫(反正它不是一個大的數據庫),那麼執行不必離開測試函數?這是可能的,我將如何實現它?訪問實體數據庫的單元測試函數
public void RetreiveKeyFnTest()
{
StegApp target = new StegApp(); // TODO: Initialize to an appropriate value
string username = "david"; // TODO: Initialize to an appropriate value
string password = "david1"; // TODO: Initialize to an appropriate value
string ConnectionString = ConfigurationManager.ConnectionStrings["DatabaseEntities"].ToString();
var dataContext = new DatabaseEntities(ConnectionString);
var user = dataContext.Users.FirstOrDefault(u => u.Username.Equals(username) && u.Password.Equals(password));
Assert.IsNotNull(user);
//target.RetreiveKeyFn(username, password);
//Assert.IsInstanceOfType(target.RetreiveLogs,typeof(DataAccess));
//Assert.IsInstanceOfType(target.p);
//Assert.IsNotNull(target.RetreiveLogs.AuthenitcateCredentials(username,password));
//Assert.Inconclusive("A method that does not return a value cannot be verified.");
}
下面是代碼我試圖測試:
public void RetreiveKeyFn(string username, string password)
{
BusinessObjects.User p = RetreiveLogs.AuthenitcateCredentials(username,password);
if (p != null)
{
if (RetreiveLogs.RetreiveMessages(p.UserId) == null)
{
DisplayLogs.Text = "Sorry No messages for you recorded in Database, your correspondant might have chose not to record the entry";
}
else
{
MessageBox.Show("LogId = " + RetreiveLogs.RetreiveMessages(p.UserId).LogId + "\n" +
"UserId = " + RetreiveLogs.RetreiveMessages(p.UserId).UserId + "\n" +
"Message Key = " + RetreiveLogs.RetreiveMessages(p.UserId).MessageKey + "\n" + "PictureId = " + RetreiveLogs.RetreiveMessages(p.UserId).PictureId +
" Date & time = " + RetreiveLogs.RetreiveMessages(p.UserId).SentDateTime);
DisplayLogs.Visible = true;
}
}
else
{
MessageBox.Show("Please enter your correct username and password in order to retreive either key, image or both from Databse");
}
}
如果停在Linq聲明中,它與'Drama'無關,它與你編碼的可能性有關。你可以提供一個正在拋出的異常,如果有的話......?當你調試這個代碼時'ConnectionString'的值是什麼? – MethodMan
例如,不應該在'dataContext.Connection.Open()'上調用Open方法..連接字符串在配置文件中看起來像什麼..? – MethodMan
請讓我知道,你想測試什麼和平(單位)的代碼? –