美好的一天,b $ 我写了一个azure函数v1 HttpTrigger,它从我们的api服务获取客户端详细信息和事务,只有一个参数是"频率" ;这也是可选的,所以当功能被触发时,它将获得详细信息然后获得每个零售商详细信息的交易并返回每个零售商的交易费用列表,我想为我的功能编写单元测试但是我我无法看到我的场景的一个好例子,有人可以给我一个示例单元测试(如果可能的话,使用moq),这里是函数代码库示例:
Good Day, I wrote an azure function v1 HttpTrigger that gets client details and transactions from our api service, there is only one parameter which is "frequency" which is optional also, so when the function is triggered it will get the details then get transactions for each retailer details and returns a list of transaction fee for each retailer, I want to write a unit test for my function but I am not able to see a good example for my scenario , Can someone give me an example unit test (with moq if possible) , here is the function codebase example:
[FunctionName("FunctionTest1")]public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function)]HttpRequestMessage req, ILogger log) { log.LogInformation("C# HTTP trigger function processed a request."); #region Properties string Frequency = req.GetQueryNameValuePairs().FirstOrDefault(q => string.Compare(q.Key, "frequency", true) == 0).Value; #endregion log.LogInformation("Getting client details from MongoDB"); Process of Getting ClientDetails log.LogInformation("Get and compute Transaction Fee foreach retailers from client details"); Process of Getting And Computing Transactions for each retailers log.LogInformation("Return results response"); return txnList == null ? new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(JsonConvert.SerializeObject("Error getting data from server"), Encoding.UTF8, "application/json") } : new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(JsonConvert.SerializeObject(txnList, Newtonsoft.Json.Formatting.Indented), Encoding.UTF8, "application/json") }; }我试过的单元测试参考: docs.microsoft/en-us/azure/azure-functions/functions-test-a功能 medium/@tsuyoshiushio/writing-unit-test-for-azure-durable-functions-80f2af07c65e $
References for unit test I tried:docs.microsoft/en-us/azure/azure-functions/functions-test-a-function medium/@tsuyoshiushio/writing-unit-test-for-azure-durable-functions-80f2af07c65e
推荐答案
我不确定这是否是最好的方法,但我确实想出了一些"只是"的东西。工作。我希望它至少可以帮助你开始
I'm not sure if this is the best way to do this but I did manage to come up with something that "just" worked. I hope it would at least help you to start off using Microsoft.Azure.WebJobs.Host; using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Diagnostics; using System.Net.Http; using System.Text; using UnitTestV1; namespace UnitTestV1Test { [TestClass] public class UnitTest1 { protected TraceWriter log = new VerboseDiagnosticsTraceWriter(); [TestMethod] public async void TestFunction1_Query() { var expected = "\"Hello Azure\""; // This seems to because the response is converted to valid JSON var res = await Function1.Run(CreateReq("localhost/func?name=Azure", ""), log); var actual = await res.Content.ReadAsStringAsync(); Assert.AreEqual(expected, actual, "Failed!"); } [TestMethod] public async void TestFunction1_Body() { var expected = "\"Hello Azure\""; // This seems to because the response is converted to valid JSON var res = await Function1.Run(CreateReq("", "{\"name\": \"Azure\"}"), log); var actual = await res.Content.ReadAsStringAsync(); Assert.AreEqual(expected, actual, "Failed!"); } public HttpRequestMessage CreateReq(string uri, string body) { var req = new HttpRequestMessage(); req.SetConfiguration(new System.Web.Http.HttpConfiguration()); if (!String.IsNullOrEmpty(uri)) { req.RequestUri = new Uri(uri); } if (!String.IsNullOrEmpty(body)) { req.Content = new StringContent(body, Encoding.UTF8, "application/json"); } return req; } } public class VerboseDiagnosticsTraceWriter : TraceWriter { public VerboseDiagnosticsTraceWriter() : base(TraceLevel.Verbose) { } public override void Trace(TraceEvent traceEvent) { Debug.WriteLine(traceEvent.Message); } } }
更多推荐
如何为azure函数v1编写单元测试
发布评论