我試圖讓我的控制器單元測試返回一個模擬文件時Request.Files[0]
被稱爲MVC犀牛嘲笑爲Request.Files
從這個網站其他職位我已經把:
[TestMethod]
public void CreateFileInDatabase()
{
var repository = new MocRepository();
var controller = GetController(repository);
HttpContextBase mockHttpContext = MockRepository.GenerateMock<HttpContextBase>();
HttpRequestBase mockRequest = MockRepository.GenerateMock<HttpRequestBase>();
mockHttpContext.Stub(x => x.Request).Return(mockRequest);
mockRequest.Stub(x => x.HttpMethod).Return("GET");
var filesMock = MockRepository.GenerateMock<HttpFileCollectionBase>();
var fileMock = MockRepository.GenerateMock<HttpPostedFileBase>();
filesMock.Stub(x => x.Count).Return(1);
mockRequest.Stub(x => x.Files).Return(filesMock);
var t = mockHttpContext.Request;
var automobile = new Automobile{ automobileNumber = "1234" };
controller.ControllerContext = new ControllerContext(mockHttpContext, new RouteData(), controller);
controller.Create(automobile);
}
當我在測試期間在控制器中,並呼籲Request.Files
我得到filesMock偉大。
但是我希望能夠撥打Request.Files[0]
並獲得一個模擬文件,我可以將其作爲參數傳遞給方法。
我沒有做過太多的嘲笑之前,所以任何幫助將不勝感激!
非常感謝堆! – AnonyMouse