views:

204

answers:

1

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?

+2  A: 

Mock HttpFileCollectionBase instead of HttpFileCollectionWrapper:

var filesMock = MockRepository.GenerateMock<HttpFileCollectionBase>();
filesMock.Stub(x => x.Count).Return(1);
mockRequest.Stub(x => x.Files).Return(filesMock);
Darin Dimitrov