尝试对访问实体框架的单元测试功能进行单元化。所以我试着将所有实体代码放入下面的测试函数中?然而,它停在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");
}
}
答案 0 :(得分:2)
首先,您应该能够在测试应用程序中访问与您在主/实际应用程序中使用的数据库相同的数据库。您只需要确保您的Test项目在其自己的App.config中包含您的连接字符串。
上下文的初始化应该在你的StegApp()里面完成,或者你应该能够从不同的范围向你的StegApp()传递一个上下文。根据我读到的代码,您的StegApp()将无法访问您创建的dataContext变量。
您对null用户的测试已经发生在AuthenticateCredentials()方法下的RetrieveKeyFn()内部,因此不需要第一个“Assert.IsNotNull(user)”。我建议将RetrieveKeyFn的业务逻辑与UI行为分开,以便您可以轻松地进行单元测试。您可以绑定“Messagebox”操作来说一个按钮单击事件处理程序,它只调用RetrieveKeyFn()。我建议也许是这样的:
public class StegApp
{
public DatabaseEntities context;
//other properties
public StegApp()
{
//assuming your DatabaseEntities class inherits from DbContext.
//You should create other constructors that allow you to set options
//like lazy loading and mappings
this.context = new DatabaseEntities();
}
//ASSUMING YOUR RetrieveLogs.RetrieveMessages() function returns
//a Message object. replace this type with whatever type the
//RetrieveLogs.RetrieveMessages() method returns.
public Message RetrieveKeyFn (string username, string password)
{
BusinessObjects.User p = RetreiveLogs.AuthenitcateCredentials(username,password);
if (p != null)
{
var message = RetrieveLogs.RetrieveMessages(p.UserId);
if (message == null)
// handle behavior for no messages. In this case
// I will just create a new Message object with a -1 LogId
return new Message {LogId =-1};
else
return message;
}
else
//handle behavior when the user is not authenticated.
//In this case I throw an exception
throw new Exception();
}
//on your button click handler, do something like:
// try
// {
// var message = RetrieveKeyFn(txtUsername.Text.Trim(), txtPassword.Text.Trim());
// if (message.LogId == -1)
// DisplayLogs.Text = "Sorry No messages for you recorded in Database, your correspondant might have chose not to record the entry";
// else
// {
// MessageBox.Show("Log Id = " + message.LogId)
// etc. etc. etc.
// }
// }
// catch
// {
// MessageBox.Show ("user is not authenticated");
// }
}
进行单元测试时,请记住在测试项目的App.Config中有适当的配置字符串如果app.config尚不存在,请继续创建一个。你应该为所有可能性创建测试(例如1)用户有效,你得到消息,2)用户有效,没有消息,3)用户无效)。
以下是案例2
的示例 [TestMethod]
public void RetrieveKeyFnTest1()
{
StegApp target = new StegApp(); // this creates your context. I'm assuming it also creates your RetrieveLogs object, etc
var username = "UserWithNotMessages"; //this user should exist in your database but should not have any messages. You could insert this user as part of your TestInitialize method
var password = "UserWithNotMessagesPassword"; //this should be the proper password
var message = target.RetrieveKeyFn(username, password);
Assert.AreEqual (-1, message.LogId);
}
答案 1 :(得分:0)
我的单元测试工作正常。我遇到的错误是不将app.config文件复制到测试项目中!虽然说实话,我预计Visual studio会做到这一点。