Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save viviandeveloper/4bf161fdd6a9692d202888130e84df6b to your computer and use it in GitHub Desktop.
Save viviandeveloper/4bf161fdd6a9692d202888130e84df6b to your computer and use it in GitHub Desktop.
A workaround for testing against CloudTable. Although CloudTable has a lot of virtual methods that can be easily mocked some examples don't work with dynamic mocking such as CreateQuery<T> with System.Linq, but there is a way!
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos.Table;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using RichardSzalay.MockHttp;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AzureFunctionTesting
{
public class Azure_Function_Test_With_Table_Binding
{
[Fact]
public void Should_be_able_to_stub_out_a_CloudTable()
{
var storageAccount = StorageAccount.NewFromConnectionString("UseDevelopmentStorage=true");
var client = storageAccount.CreateCloudTableClient();
var mockedRequest = new MockHttpMessageHandler()
.When("http://127.0.0.1:10002/devstoreaccount1/pizzas*")
.Respond("application/json",
@"{
""value"": [
{
""Name"": ""Pepperoni"",
""Price"": 9.99
}
]
}");
client.TableClientConfiguration.RestExecutorConfiguration.DelegatingHandler = new MockRequestAdapter(mockedRequest);
var table = client.GetTableReference("pizzas");
var request = new DefaultHttpContext().Request;
request.Query = new QueryCollection(new Dictionary<string, StringValues> { { "Pizza", new StringValues("Pepperoni") } });
var result = PizzaStore.Run(request, table, null);
Assert.IsType<OkObjectResult>(result);
}
}
public class MockRequestAdapter : DelegatingHandler
{
private readonly MockedRequest _mockedRequest;
public MockRequestAdapter(MockedRequest mockedRequest) : base()
{
_mockedRequest = mockedRequest;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
return await _mockedRequest.SendAsync(new HttpRequestMessage(request.Method, request.RequestUri), cancellationToken);
}
}
public static class PizzaStore
{
[FunctionName("PizzaStore")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
[Table("pizzas", Connection = "AzureWebJobsStorage")] CloudTable cloud,
ILogger log)
{
if (req.Query.TryGetValue("Pizza", out var value))
{
var pizza = cloud.CreateQuery<Pizza>().Where(p => p.Name == value.ToString()).SingleOrDefault();
return new OkObjectResult(new { Pizza = pizza.Name, Price = pizza.Price });
}
return new NotFoundResult();
}
}
public class Pizza : TableEntity
{
public string Name { get; set; }
public double Price { get; set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment