Unit Test Friendly File Upload In ASP.NET

The Problem

In an N-tier application, you keep your logic in a business logic tier, typically a different VS project that can be used from a website, a windows service, or desktop application, and that should be valid to writ unit tests against on its own.

But how about if your requirements say that you need to to upload some file for the business logic to work? Think of a scanned image (signed contract maybe?) or just a comma separated value file containing some emails.  Typically the business logic tier will be the place to to handle this, but how can you send the uploaded file to it? You can get the file as “HttpPostedFile” from the “Request.Files” collection or the file upload control itself, but, to receive it in the business logic, project, the easiest way is to add reference to “System.Web” dll and accept the type “HttpPostedFile” as method argument or so, and when in need to save the file physically, you call the “SaveAs” method of “HttpPostedFile” … So simple right ?

But how about unit testing? “HttpPostedFile” has no public constructor and is sealed class (you cannot inherit from). How will you write a unit test for a method that accepts “HttpPostedFile” as argument to work? You’d go finding a way to mock that or just forget about testing that particular method!!

A Simple Solution

Well, it’s more simple than you think. An “HttpPostedFile” has a property called “InputStream”, which is of type “System.IO.Stream”. Hey, that has nothing to do with “system.Web” :). You can create your own FileStream or whatever other stream in the unit test, and then only worry about other areas you want to test related to the method in your business logic.

Because I know dealing with streams is quite ugly (at least it is to me!), I wrote an example that will walk you though a sample usage of that property. I have the complete example VS solution as attached file at the end of the example code below.

The Business Logic

In a real world example, the business class will have various parameters including the file, but in this example, I have made my business class (I call it “FileLogic”) just worry about the file. It has a method that treats the stream coming to it as containing just text, reads that text and returns it. Note the type of the only method parameter

Another example of a method in the business logic project is one that actually saves the file. Instead of just calling “SaveAs(filepath)”, you need to copy the stream yourself. Usually there’s more than one way to do so, the problem in most of the ways is determining the size of the stream. An “HttpPostedFile” knows the size of the file, but as I’m not getting the “HttpPostedFile” (and it wouldn’t sound great if I requested the file size as a method parameter!), I dealt as if I do not know the actual file size. See the code for this method:

Example: A Webforms ASP.NET Project

Before we dig into the unit test sample, let’s see how our page code will look when dealing with this business logic class.

Consider a page with controls like these:

The code behind for such page with our custom logic class (in a sample manner still), would look like this:

I’d say that this part is not much different than it’d be if we were passing the “HttpPostedFile” object complete.

The Unit Test

So, that’s what we have been hassling, complicating code, and dealing directly with streams for its sake!

As our application will not necessarily be a file sharing application, we’ll be in need for the file as part of a bigger operation. A unit test will not be only concerned with the file upload task, but also the rest parts of the business operation our method intends to do. However, in this sample, I’ve decided to make it just test the file uploading functionality, and included really simple ways in the unit test.

Instead of the file upload, I used a physical file that I get its path from a method called “GetTestFilename” (I could have used configuration file, static property or else for the file stream, or used any other kind of stream still).

See how the test methods look in this example (using NUnit):

Share With Friends:

P.S. Please help me out by checking this offer, then look below for a small Thank You.

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.
It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2, ASP.NET 5, and other fullstack developer goodies.

  • There are tools that do this out of the box like Web test in VSTS, and I am sure that there are open source products that do this too.

  • We had this conversation on the phone but just for the record here :D :D :D

    This is not about automating web testing, but about solving a situation where most people would add dependency on web components that are created deep inside ASP.NET, clearing that the specified dependency simply does not have to be added :).

  • Pingback: orologi uomo cartier()