如何在运行单元测试时避免调用此额外方法?我想以某种方式让构造函数中创建的上下文对象用于单元测试
[TestMethod]
public void Delete_Sp_List()
{
ctx = tf.GetContext();
List list = ctx.Web.Lists.GetByTitle("StackTicketList");
list.DeleteObject();
ctx.ExecuteQuery();
}
public TicketForm()
{
SecureString ssPwd = new SecureString();
strPassword.ToList().ForEach(ssPwd.AppendChar);
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(strUserName, ssPwd);
ctx.Credentials = credentials;
}
public ClientContext GetContext()
{
SecureString ssPwd = new SecureString();
strPassword.ToList().ForEach(ssPwd.AppendChar);
SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(strUserName, ssPwd);
ctx.Credentials = credentials;
return ctx;
}
答案 0 :(得分:1)
您应该查看[TestInitilize]属性。您可以创建一个方法(void Init(){...})并使用此属性标记它。在执行每个测试方法之前将调用此方法。通过将初始化逻辑放在Init方法中,可以避免在测试方法之间复制此逻辑
[TestClass]
public class Test
{
private ClientContext ctx;
[TestInitialize]
public void Init()
{
ctx = GetContext();
}
[TestMethod]
public void Delete_Sp_List()
{
List list = ctx.Web.Lists.GetByTitle("StackTicketList");
list.DeleteObject();
ctx.ExecuteQuery();
}
}