Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rhino Mocks: Mocking HttpRequestBase.Files

I have a view & controller that allow the user to upload a file to the server. This is done in the view using an input type='file' and in the controller by getting the value of Request.Files (HttpRequestBase, returning a HttpFileCollectionWrapper).

I am having difficulty mocking this using Rhino Mocks.

HttpContextBase mockHttpContext = MockRepository.GenerateMock<HttpContextBase>();
HttpRequestBase mockRequest = MockRepository.GenerateMock<HttpRequestBase>();
mockHttpContext.Stub(x => x.Request).Return(mockRequest);

mockRequest.Stub(x => x.HttpMethod).Return("GET");

// Next line fails -  throws MissingMethodException
// (Can't find a constructor with matching arguments)
HttpFileCollectionWrapper files =
    MockRepository.GenerateMock<HttpFileCollectionWrapper>();

files.Stub(x => x.Count).Return(1);

mockRequest.Stub(x => x.Files).Return(files);

The constructor for HttpFileCollectionWrapper requires an HttpFileCollection, however this has an internal constructor.

Can anyone suggest how to get this approach, or a variant of it, to work?

like image 589
Richard Ev Avatar asked Feb 22 '10 09:02

Richard Ev


1 Answers

Mock HttpFileCollectionBase instead of HttpFileCollectionWrapper:

var filesMock = MockRepository.GenerateMock<HttpFileCollectionBase>();
filesMock.Stub(x => x.Count).Return(1);
mockRequest.Stub(x => x.Files).Return(filesMock);
like image 195
Darin Dimitrov Avatar answered Oct 22 '22 10:10

Darin Dimitrov