开发者

Controller Test problems

开发者 https://www.devze.com 2022-12-12 23:19 出处:网络
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(\"~/Te开发者_如何学Pythonmp\")(\"this\"

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("~/Te开发者_如何学Pythonmp") ("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


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.


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号