Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nickraphael opened this issue Feb 14, 2023 · 2 comments

Comments

@nickraphael
Copy link

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

@dannoh
Copy link

dannoh commented Feb 27, 2023

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
Copy link

johngrant commented Mar 30, 2023

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 })); 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants