tiagodaraujo / HttpContextMoq

MIT License
34 stars 14 forks source link

Are there plans to add files to the mock object? For testing file uploads. #6

Open nickraphael opened 1 year ago

nickraphael commented 1 year ago

Currently there doesn't seem to be a way to mock a context that holds a file.

dannoh commented 1 year ago

I believe this works

var files = new List<IFormFile>();
mockHttpContext.RequestMock.FormMock.FilesMock.Mock.Setup(c => c.GetEnumerator()).Returns(files.GetEnumerator());

You could also mock the indexer if you needed to.

johngrant commented 1 year ago

For me I setup the ReadFormAsync() that was used in my controller to get the Forms and then the Files.

var file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 20, "Data", "dummy.txt"); 
httpContextMock.RequestMock.Mock.Setup(s => s.ReadFormAsync(It.IsAny<CancellationToken>())).ReturnsAsync(new FormCollection(new Dictionary<string, StringValues>(), new FormFileCollection { file }));