Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing ASP.NET MVC website

I'm working on an MVC site with an image upload capability and I want to write a test that will upload an image.

I made an image called TestImage.jpg and set Copy to Output to be "Copy if Newer". In my test I try to load that with the following code:

System.Drawing.Image testImage = System.Drawing.Image.FromFile(@"TestImage.jpg");

Shouldn't the "Copy to Output" copy it to the same directory where the test is running? If not, how can I find out where it was copied to? Best would be some kind of project root relative path so I can feel free to move the solution around without this breaking.

like image 553
John Hoge Avatar asked Sep 28 '09 22:09

John Hoge


People also ask

What is ASP.NET MVC unit testing?

In computer programming, unit testing is a software testing method by which individual units of source code are tested to determine whether they are fit for use.

How difficult is ASP.NET MVC?

It's really difficult to try and learn an entirely new language/framework under pressure. If you're required to deliver working software for your day job, trying to learn ASP.NET Core at the same time might be heaping too much pressure on yourself.

Is ASP.NET MVC still in use?

It is no longer in active development. It is open-source software, apart from the ASP.NET Web Forms component, which is proprietary. ASP.NET Core has since been released, which unified ASP.NET, ASP.NET MVC, ASP.NET Web API, and ASP.NET Web Pages (a platform using only Razor pages).


1 Answers

In MSTest, the framework copies all .dlls to a folder called TestResults. Unfortunately, it only copies .dlls, .pdbs and .configs from the output folder to the TestResults folder, so your files are not being copied.

To copy those files, you will need to either

  • add the DeploymentItem attribute to the test(s) in question
  • edit your .testrunconfig file by adding the appropriate files in the Deployment tab

A better alternative is to embed the test-specific files in the test as an embedded resource and then read them directly from the resource stream.

like image 107
Mark Seemann Avatar answered Nov 15 '22 10:11

Mark Seemann