Skip to content

Instantly share code, notes, and snippets.

@TsuyoshiUshio
Last active April 21, 2023 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save TsuyoshiUshio/d4e619f9123a9b521ac3cbe6804cc647 to your computer and use it in GitHub Desktop.
Save TsuyoshiUshio/d4e619f9123a9b521ac3cbe6804cc647 to your computer and use it in GitHub Desktop.
Azure Functions Unit Testing sample
[FunctionName(“HttpTrigger”)]
public async static Task<IActionResult> RunAsync([HttpTrigger(AuthorizationLevel.Function, “get”, “post”, Route = null)]HttpRequest req, TraceWriter log)
{
log.Info(“C# HTTP trigger function processed a request.”);
string name = req.Query[“name”];
string requestBody = new StreamReader(req.Body).ReadToEnd();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;
return name != null
? (ActionResult)new OkObjectResult($”Hello, {name}”)
: new BadRequestObjectResult(“Please pass a name on the query string or in the request body”);
}
[TestClass]
public class HttpTriggerTest : FunctionTestHelper.FunctionTest
{
[TestMethod]
public async Task Request_With_Query()
{
var query = new Dictionary<String, StringValues>();
query.TryAdd(“name”, “ushio”);
var body = “”;
var result = await HttpTrigger.RunAsync(req: HttpRequestSetup(query, body), log: log);
var resultObject = (OkObjectResult)result;
Assert.AreEqual(“Hello, ushio”, resultObject.Value);
:
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment