public void CreateMethod() { // preparation var cosmos = new Mock <ICosmos>(); var notification = new Mock <INotificationService>(); var logger = new Mock.LoggerMock <Covid19Radar.Api.External.NotificationCreateApi>(); var notificationCreateApi = new Covid19Radar.Api.External.NotificationCreateApi(cosmos.Object, notification.Object, logger); }
public async Task RunAsyncMethod(string title, string message, System.Type ResultType) { // preparation var result = new Mock <ItemResponse <NotificationMessageModel> >(); result.SetupGet(_ => _.Resource) .Returns(new NotificationMessageModel() { Message = message }); var cosmosNotification = new Mock <Container>(); cosmosNotification.Setup(_ => _.CreateItemAsync(It.IsAny <NotificationMessageModel>(), It.IsAny <PartitionKey?>(), It.IsAny <ItemRequestOptions>(), It.IsAny <CancellationToken>())) .ReturnsAsync(result.Object); var cosmos = new Mock <ICosmos>(); cosmos.SetupGet(_ => _.Notification) .Returns(cosmosNotification.Object); var notification = new Mock <INotificationService>(); var logger = new Mock.LoggerMock <Covid19Radar.Api.External.NotificationCreateApi>(); var notificationCreateApi = new Covid19Radar.Api.External.NotificationCreateApi(cosmos.Object, notification.Object, logger); var context = new Mock.HttpContextMock(); var param = new Api.External.Models.NotificationCreateParameter() { Title = title, Message = message }; var bodyString = Newtonsoft.Json.JsonConvert.SerializeObject(param); using var stream = new System.IO.MemoryStream(); using (var writer = new System.IO.StreamWriter(stream, leaveOpen: true)) { await writer.WriteAsync(bodyString); await writer.FlushAsync(); } stream.Seek(0, System.IO.SeekOrigin.Begin); context._Request.Body = stream; // action var actual = await notificationCreateApi.RunAsync(context.Request); // assert Assert.IsInstanceOfType(actual, ResultType); }