コード例 #1
0
        public async Task TestUploadFileAsync_Uploaded()
        {
            IAttachmentAppService factory(string id)
            {
                var appService = Substitute.For <IAttachmentAppService>();

                appService.GetByIdAsync(id)
                .ReturnsForAnyArgs(new AttachmentItem
                {
                    Id     = id,
                    Status = UploadStatus.Uploaded
                });
                return(appService);
            };
            var storeService = Substitute.For <IAttachmentStoreService>();
            var formFile     = Substitute.For <IFormFile>();
            var target       = new AttachmentController(factory, storeService,
                                                        Substitute.For <IMemoryCache>());
            var vm = new UploadVm
            {
                ContextId = Guid.Empty,
                File      = formFile,
                MD5       = Guid.NewGuid().ToString("N")
            };
            var result = await target.UploadFileAsync(vm);

            var responseData = result.Value;

            responseData.Data.Should().Be(UploadStatus.Uploaded);
            await storeService.DidNotReceiveWithAnyArgs().StoreFileAsync(vm, DateTimeOffset.UtcNow);
        }
コード例 #2
0
        public async Task TestUploadFileAsync_Stored()
        {
            var formFile = Substitute.For <IFormFile>();
            var vm       = new UploadVm
            {
                ContextId = Guid.Empty,
                File      = formFile,
                MD5       = Guid.NewGuid().ToString("N")
            };

            var appService = Substitute.For <IAttachmentAppService>();

            appService.AddOrUpdateAsync(Arg.Any <AttachmentItem>())
            .Returns(Task.CompletedTask);
            IAttachmentAppService factory(string id) => appService;

            var storeService = Substitute.For <IAttachmentStoreService>();
            var now          = DateTimeOffset.UtcNow;

            storeService.StoreFileAsync(vm, Arg.Any <DateTimeOffset>())
            .Returns("abc.ext");

            var target = new AttachmentController(factory, storeService,
                                                  Substitute.For <IMemoryCache>());

            target.ControllerContext = CreateMockContext();

            var result = await target.UploadFileAsync(vm);

            var responseData = result.Value;

            Assert.AreEqual(UploadStatus.Uploaded, responseData.Data);

            await appService.Received().AddOrUpdateAsync(Arg.Is <AttachmentItem>(o =>
                                                                                 o.Id == vm.MD5 && o.Location == "abc.ext"
                                                                                 ));
        }