c# - Write form data to OwinRequest in unit test -


as part of unit test, i'm attempting create mock iowinrequest contains http form data body. code in question works in production (i can read form data requests sent through web browser), can't understand i'm doing wrong unit test:

[testmethod] public async task testpostdata() {     string rawformdata = "test=testvalue&test2=testvalue2";     memorystream stream = new memorystream();      using (streamwriter writer = new streamwriter(stream, encoding.utf8, 4 * 1024, true))         await writer.writeasync(rawformdata);      stream.seek(0, seekorigin.begin);      mock<iowinrequest> requestmock = new mock<iowinrequest>();     requestmock.setupget(r => r.method).returns("post");     requestmock.setupget(r => r.body).returns(stream);     requestmock.setupget(r => r.contenttype).returns("application/x-www-form-urlencoded");      assert.isnotnull(await requestmock.object.readformasync()); } 

the form object returned readformasync method null. missing? on .net4.5 , microsoft.owin 3.0.1

you have not setup method return null when called.

[testmethod] public async task _testpostdata() {     var dictionary = new dictionary<string, string[]>() {          { "test", new[]{"testvalue"}},          { "test2", new[]{"testvalue2"}},      };     string rawformdata = "test=testvalue&test2=testvalue2";     var stream = new memorystream();      using (var writer = new streamwriter(stream, encoding.utf8, 4 * 1024, true))         await writer.writeasync(rawformdata);      stream.seek(0, seekorigin.begin);      var formmock = new mock<iformcollection>();     formmock.setup(_ => _.getenumerator()).returns(() => dictionary.getenumerator());     var readablestring = formmock.as<ireadablestringcollection>();     //..setup desired       var requestmock = new mock<iowinrequest>();     requestmock.setup(_ => _.method).returns("post");     requestmock.setup(_ => _.body).returns(stream);     requestmock.setup(_ => _.contenttype).returns("application/x-www-form-urlencoded");     requestmock.setup(_ => _.readformasync()).returnsasync(formmock.object);      assert.isnotnull(await requestmock.object.readformasync()); } 

but given question appears xy problem. there not more in terms of setup can provide in answer.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -