我有一个 View 和 Controller ,允许用户将文件上传到服务器。这是在 View 中使用input type='file'以及在 Controller 中通过获取Request.Files的值(HttpRequestBase,返回HttpFileCollectionWrapper)完成的。

我在使用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);
HttpFileCollectionWrapper的构造函数需要HttpFileCollection,但是它具有internal构造函数。

谁能建议如何使这种方法或其变体起作用?

最佳答案

模拟HttpFileCollectionBase而不是HttpFileCollectionWrapper:

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

关于asp.net-mvc - 犀牛Mo : Mocking HttpRequestBase.文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2310024/

10-09 01:11