Skip to content

Instantly share code, notes, and snippets.

@chrisnicola
Created January 7, 2010 18:18
Show Gist options
  • Save chrisnicola/271427 to your computer and use it in GitHub Desktop.
Save chrisnicola/271427 to your computer and use it in GitHub Desktop.
protected static HttpPostedFileBase MockFileUpload(ControllerBase controller)
{
var request = MockRepository.GenerateStub<HttpRequestBase>();
var context = MockRepository.GenerateStub<HttpContextBase>();
var server = MockRepository.GenerateStub<HttpServerUtilityBase>();
var postedfile = MockRepository.GenerateMock<HttpPostedFileBase>();
var postedfilesKeyCollection = MockRepository.GenerateStub<HttpFileCollectionBase>();
var fakeFileKeys = new List<string> { "image", "file" };
context.Stub(ctx => ctx.Request).Return(request);
context.Stub(ctx => ctx.Server).Return(server);
server.Stub(s => s.MapPath(null)).IgnoreArguments().Return("/Path/");
request.Stub(req => req.Files).Return(postedfilesKeyCollection);
postedfilesKeyCollection.Stub(keys => keys.GetEnumerator()).Return(fakeFileKeys.GetEnumerator());
postedfilesKeyCollection.Stub(keys => keys["file"]).Return(postedfile);
postedfilesKeyCollection.Stub(keys => keys["image"]).Return(postedfile);
postedfile.Expect(f => f.FileName).Return("foo.doc").Repeat.Any();
postedfile.Expect(f => f.SaveAs(Arg<string>.Is.Anything)).Repeat.Any();
postedfile.Expect(f => f.ContentLength).Return(512).Repeat.Twice();
controller.ControllerContext = new ControllerContext(context, new RouteData(), controller);
return postedfile;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment