c# - How to upload a file to an endpoint from an integration test -
when file uploaded client, can found in this.request.files, of type httpfilecollectionbase.
httpfilecollectionbase contains httppostedfilebase entries.
the properties on these objects read-only, hoping setup mocks. mock request return mock httpfilecollection, , collection contain 1 mockhttppostedfile. inputstream property on object return filestream object instantiate using real file in source control.
- is approach uploading file endpoint integration test?
- if approach, how can mock out
moq? - if not approach, can suggest better way?
thanks!
i know nothing asp.net mvc, looks need setup mock dependencies:
memorystream stream = new memorystream(); var mockfile = new mock<httppostedfilebase>(); var mockfiles = new mock<httpfilecollectionbase>(); var mockrequest = new mock<httprequestbase>(); mockfile.setup(f => f.inputstream).returns(stream); // if example, index file name. mockfiles.setup(f => f[it.isany<string>()]).returns(mockfile.object); mockrequest.setup(r => r.files).returns(() => mockfiles.object); // write expected data memory stream, instantiate class // under test using mockrequest.object in unit test, use memory stream rather file, file stream works same.
if want avoid having mock out , set these dependencies, put them behind uploadedfiles abstraction, make code depend on abstraction, , mock uploadedfiles. need thin wrapper around request.files file stream name (or access them). better because downstream code depends on uploadedfiles, stream , string, rather httprequestbase, httpfilecollectionbase, httppostedfilebase, stream , string.
setup simplified this:
memorystream stream = new memorystream(); var mockuploadedfiles = new mock<uploadedfiles>(); mockuploadedfiles.setup(u => u.getfile(it.isany<string>())).returns(stream);
Comments
Post a Comment