Take a look at some of the tests in MVCContrib. You need to mock out Response, Request and a bunch of other stuff. It's not pretty but doable
On Mon, Nov 17, 2008 at 9:01 AM, chiragkumar maisuriya < [EMAIL PROTECTED]> wrote: > Hi Justin , > > Can I use Unity from Codplex ? > > Thanks & Regards, > Chirag > > > > On Mon, Nov 17, 2008 at 7:54 PM, Justin Chase <[EMAIL PROTECTED]>wrote: > >> >> FYI, a quick list of IOC Containers: >> >> http://csharp-source.net/open-source/containers >> >> >> On Nov 17, 8:21 am, "Justin Chase" <[EMAIL PROTECTED]> wrote: >> > I don't know if you can effectively mock the HttpContext. It's sealed >> and >> > has no interfaces... it's one massive side effect. I would recommend >> > abstracting your use of it out to a custom class that wraps it and helps >> you >> > break direct dependencies on it. >> > >> > One way I would recommend to do this is by using an IOC container and >> > abstracting out the http context. So suppose all you want is the current >> > User off of the HttpContext. Instead of directly referencing the context >> > make an interface and class like this: >> > public interface IHttpContext >> > { >> > IPrincipal User { get; } >> > >> > } >> > >> > public class HttpContextWrapper : IHttpContext >> > { >> > HttpContext _context; >> > public HttpContextWrapper(HttpContext context) >> > { >> > _context = context; >> > } >> > >> > public IPrincipal User { get { return _context.User; } } >> > >> > } >> > >> > Now in your code that used to call the context to get the user do >> something >> > like this: >> > >> > IHttpContext context = Container.Create<IHttpContext>(); >> > IPrincipal user = context.User; >> > >> > Now to register it in your real code, at app initialization: >> > >> > Container.Register<IHttpContext>(() => return new >> > HttpContextWrapper(HttpContext.Current)); >> > >> > and using Rhino Mocks in your unit test library instead register it this >> > way: >> > >> > IHttpContext context = MockRepository.GenerateMock<IHttpContext>(); >> > Container.Register<IHttpContext>(() => return context); >> > >> > The exact syntax for the container is just psuedo code above, it depends >> on >> > the framework you're using but the concepts are generally the same. You >> use >> > the container as a factory method to break dependencies between types. >> In >> > the real app you register the real types in the test app you register >> mock >> > types. >> > >> > On Mon, Nov 17, 2008 at 1:40 AM, chiragkumar maisuriya < >> > >> > >> > >> > [EMAIL PROTECTED]> wrote: >> > >> > > Hi Ayende, >> > >> > >> I am working on Rhino Mocks and trying to mock out the HttpContext >> for one >> > >> of my Controller's Method using rhino Mocks but Finding a lot of >> troubles >> > >> > >> I am sending the my Method and Test Method. Please Suggest me some >> way so >> > >> that I can mock out HttpContext Object for Unit Test my method. >> > >> > >> public override bool Execute(string moduleId, string actionId, >> > >> HttpContext context) >> > >> > >> { >> > >> > >> switch (actionId) >> > >> > >> { >> > >> > >> case "Init": >> > >> > >> LoadData(); >> > >> > >> break; >> > >> > >> default: >> > >> > >> break; >> > >> > >> } >> > >> > >> dealerBulletinDetailsEntity.ActionId = actionId; >> > >> > >> SetContext(dealerBulletinDetailsEntity, context); >> > >> > >> SetContext(rulesEntity, context); >> > >> > >> return RedirectUrl(moduleId, context); >> > >> > >> } >> > >> > >> Test Method >> > >> > >> [ >> > >> TestMethod()] >> > >> > >> public void ExecuteTest() >> > >> > >> { >> > >> > >> PolicyRulesController target = new PolicyRulesController(); // TODO: >> > >> Initialize to an appropriate value >> > >> > >> string moduleId = string.Empty; // TODO: Initialize to an appropriate >> > >> value >> > >> > >> string actionId = string.Empty; // TODO: Initialize to an appropriate >> > >> value >> > >> > >> HttpContextBase context = >> MockRepository.GenerateStub<HttpContextBase>(); >> > >> > >> bool expected = false; // TODO: Initialize to an appropriate value >> > >> > >> bool actual; >> > >> > >> actual = target.Execute(moduleId, actionId, context); >> > >> > >> Assert.AreEqual(expected, actual); >> > >> > >> Assert.Inconclusive("Verify the correctness of this test method."); >> > >> > >> } >> > >> > >> Thanks & Regards, >> > >> > >> Chirag >> > >> > -- >> > Justin Chasehttp://www.justnbusiness.com >> >> >> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Rhino.Mocks" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/RhinoMocks?hl=en -~----------~----~----~----~------~----~------~--~---
