Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6887379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:52:40+00:00 2026-05-27T05:52:40+00:00

Using MVC3.NET I have a file upload method in a controller that works fine

  • 0

Using MVC3.NET I have a file upload method in a controller that works fine with the following signature public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file)

How can I unit test this with NUnit? I have looked around and everyone seems to point to Moq but I’m new to unit testing and cannot get Moq working.

I have found interesting blogs such as this: http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html but am struggling to figure out how the same might be done to ‘fake’ a file upload, and am also wary that a lot on moq examples that I have managed to find now seem to have deprecated code in them.

I would simply like to know how I can simulate a HttpPostedFileBase so I can test my upload code, using Moq or otherwise – I would be really grateful if someone could give me some code examples on how to do this.

The following code taken from other examples on here:

var file = new Mock<HttpPostedFileBase>();
            file.Setup(f => f.ContentLength).Returns(1);
            file.Setup(f => f.FileName).Returns("test.txt");

controller.upload(file);

generates the following error when I try to compile:

cannot convert from ‘Moq.Mock’ to
‘System.Web.HttpPostedFileBase’

I have changed the method to take a singular HttpPostedFileBase for now, rather than an IEnumerable, as being able to ‘mock’ one is what I’m trying to focus on for the purpose of this question.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T05:52:41+00:00Added an answer on May 27, 2026 at 5:52 am

    Assuming a standard file upload action:

    [HttpPost]
    public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> files)
    {
        foreach (var file in files)
        {
            var filename = Path.Combine(Server.MapPath("~/app_data"), file.FileName);
            file.SaveAs(filename);
        }
        return View();
    }
    

    you could test it like this:

    [Test]
    public void Upload_Action_Should_Store_Files_In_The_App_Data_Folder()
    {
        // arrange
        var httpContextMock = new Mock<HttpContextBase>();
        var serverMock = new Mock<HttpServerUtilityBase>();
        serverMock.Setup(x => x.MapPath("~/app_data")).Returns(@"c:\work\app_data");
        httpContextMock.Setup(x => x.Server).Returns(serverMock.Object);
        var sut = new HomeController();
        sut.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), sut);
    
        var file1Mock = new Mock<HttpPostedFileBase>();
        file1Mock.Setup(x => x.FileName).Returns("file1.pdf");
        var file2Mock = new Mock<HttpPostedFileBase>();
        file2Mock.Setup(x => x.FileName).Returns("file2.doc");
        var files = new[] { file1Mock.Object, file2Mock.Object };
    
        // act
        var actual = sut.UploadFile(files);
    
        // assert
        file1Mock.Verify(x => x.SaveAs(@"c:\work\app_data\file1.pdf"));
        file2Mock.Verify(x => x.SaveAs(@"c:\work\app_data\file2.doc"));
    }
    

    Obviously all the HttpContext setup part should be externalized into a reusable class that could be called in the [SetUp] phase of your unit test to prepare the mock context of the subject under test and to avoid repeating it in every single unit test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I have an upload that stores images to a SQL 2008 from an ASP.NET
I have an ASP.Net MVC 3 application that runs fine when I use the
Here's the issue at hand: I have developed an ASP.NET MVC3 application using Razor.
I am writing a simple proof-of-concept web service using ASP.NET MVC3 controller with JSON
I have an MVC3 C#.Net web app. In my HTML View, I am using
I am using ASP.NET MVC 3. I have an action method called New. When
I am currently using the file upload helper that comes with Microsoft.WebHelpers like this
Using ASP.net MVC3 with C# I want to create a partial view that uses
I have an MVC3 C#.Net web app. In my HTML View, I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.