views:

67

answers:

3

Hi. I want to test a mvc controller. I'm using Moq to mock the services, but I don't know how to mock this.Request.Files["Attachement1"] and this.Server.MapPath("~/Temp") ("this" is the controller)

I tried to create a new Mock<HttpRequestBase>(); but this.Request doesn't have a setter. Help me please with an advise. Thanks

A: 

If you have a look at the TestHelper in the MVCContrib project, it can easily be extended to Mock other bits of the Http elements. (It has some Request elements already to use as a template.)

Kindness,

Dan

Daniel Elliott
+1  A: 

I had a similar problem as your - I used the set of fake classes from Stephen Walther's blog.

Asp.Net MVC Tips - Faking the Controller Context

I had to modify some of the classes slightly but it should do what you want and it's definitely a lot easier to setup than having to mock the entire context every time.

Jaco Pretorius
A: 

For the folder name resolutions such as: this.Server.MapPath("~/Temp") I use public properties, and getter returns this so I can easily test it. For the Request.Files, I prefer using FormCollection dictionary

VolkanUzun