intrinsics - How do I fake a user log in for unit testing purposes using fakeiteasy within asp.net mvc 2 -
i have started learning , usingasp.net mvc 2 , getting more involved unit testing code. question broadly how simulate user log in passing in credentials within test.
i using mspec , trying head around fakeiteasy in order write test. far, believe i've written 1 test correctly (it passes test condition) when unauthenticated user tries access page.
subject( typeof( homecontroller ) )] public class context_for_a_home_controller_for_not_logged_user { protected static homecontroller homecontroller; establish context = () => { // create controller homecontroller = new homecontroller(); homecontroller.controllercontext = a.fake<controllercontext>(); }; } [subject(typeof(homecontroller))] public class when_the_home_page_is_requested : context_for_a_home_controller_for_not_logged_user { static actionresult result; because of = () => result = homecontroller.index(); should_return_the_log_in_page_if_user_not_logged_in = () => { result.shouldbeaview().and().shouldusedefaultview(); }; } so far good. however, i'd test scenario when authenticated user hits home controller. i'm stuck in how simulate authenticated user , or advice welcome.
tia,
david
after getting in touch patrik hagne, creator of fakeiteasy, came following:
[subject( typeof( homecontroller ) )] public class context_for_a_home_controller_for_logged_user { protected static homecontroller homecontroller; establish context = () => { // create controller homecontroller = new homecontroller(); homecontroller.controllercontext = a.fake<controllercontext>(); var fakeprincipal = a.fake<iprincipal>(); var fakeidentity = new genericidentity( "username" ); a.callto( () => fakeprincipal.identity ).returns( fakeidentity ); a.callto( () => homecontroller.controllercontext.httpcontext.user ).returns( fakeprincipal ); }; } that did trick! patrik!
Comments
Post a Comment