当我从代码中的其他地方调用它时,我的方法工作正常,我添加了单元测试来测试这个方法,当它从测试方法调用时,它会在第一行引发异常.
public static void PostToAzureQueue(AlertNotification alertNotification)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("activealertsqueue");
queue.CreateIfNotExists();
CloudQueueMessage message = new CloudQueueMessage(alertNotification.Serialize());
queue.AddMessage(message);
}
Run Code Online (Sandbox Code Playgroud)
这是测试方法
public void VerifyPostToAzureQueue()
{
try
{
AlertNotification alertNotification = new AlertNotification();
alertNotification.DataCenters = "TestCenter";
alertNotification.TimeStamp = DateTime.Now;
Utils.PostToAzureQueue(alertNotification);
Assert.IsTrue(true);
}
catch
{
Assert.Fail();
}
}
Run Code Online (Sandbox Code Playgroud)
当我在方法的第一行硬编码连接字符串时,它传递了这一行但在第二行中失败了.当我从其他地方调用它时,该方法工作得很好.请注意,测试方法是在单独的测试项目中.