使用抽象来避免与实现问题的紧密耦合。
public interface IDBHelper {
bool UpgradeDB();
}
public class DBUPHelper: IDBHelper {
//...code omitted for brevity
}
此外,由于测试方法是静态的,因此暴露静态字段/属性
public static class MyFunction {
//At run time this will use default helper
public static IDBHelper Helper = new DBUPHelper();
private static ILogger logObj;
[FunctionName("HttpStart")]
public static async Task<HttpResponseMessage> Run(
[HttpTrigger(AuthorizationLevel.Function, methods: "post", Route = "orchestrators/{functionName}")] HttpRequestMessage req,
[OrchestrationClient] DurableOrchestrationClientBase starter,
string functionName,
ILogger log, ExecutionContext context)
{
HttpResponseMessage response = null;
if (helper.UpgradeDB()) {
log.LogInformation("DB Upgraded Successfully");
logObj = log;
try
{
var provider = new MultipartMemoryStreamProvider();
await req.Content.ReadAsMultipartAsync(provider);
Application policy = await GeneratePolicyObject(provider);
string instanceId = await starter.StartNewAsync(functionName, policy);
log.LogInformation($"Started orchestration with ID = '{instanceId}'.");
response = starter.CreateCheckStatusResponse(req, instanceId);
response.Headers.RetryAfter = new RetryConditionHeaderValue(TimeSpan.FromSeconds(10));
}
catch (Exception ex)
{
response = new HttpResponseMessage();
log.LogCritical(ex.ToString());
log.LogCritical(ex.InnerException.ToString());
log.LogCritical(ex.StackTrace);
response.Content = new StringContent(ex.ToString());
response.StatusCode = System.Net.HttpStatusCode.InternalServerError;
}
}
else log.LogCritical("DB Upgrade Failed. Check logs for exception");
return response;
}
}
可以在隔离测试时更换
public async Task TestFunction {
//Arrange
var helper = MockRepository.GenerateMock<IDBHelper>();
MyFunction.helper = helper; //<<--override default helper with mock
helper.Stub(_ => _.UpgradeDB()).Return(false);//or true is that is what you desire
//...arrange other parameters / dependencies
//Act
var actual = await MyFunction.Run(...);
//Assert
//...
}