开发者

How do I fake a user log in for unit testing purposes using fakeiteasy within asp.net mvc 2

I have just started learning and usingASP.NET MVC 2 and also getting more involved into unit testing my code. My question is broadly how to simulate a user log in by passing in credentials within my test.

I am using MSpec and trying to get my head aro开发者_开发知识库und fakeiteasy in order to write up my test. So far, I believe I've written one test correctly (It passes the test condition) for when an unauthenticated user tries to access a 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();

    It should_return_the_log_in_page_if_user_not_logged_in = () =>
        { result.ShouldBeAView().And().ShouldUseDefaultView(); };
}

So far so good. However, I'd like to test for the scenario for when an authenticated user hits the home controller. I'm stuck in how to simulate an authenticated user and any help or advice would be welcome.

TIA,

David


After getting in touch with Patrik Hagne, the creator of FakeItEasy, he came up with the 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 the trick! Thanks Patrik!


Here's how you could fake the identity:

Subject( typeof( HomeController ) )]
public class context_for_a_home_controller_for_a_logged_user
{
    protected static HomeController HomeController;

    Establish context = () =>
    {
        // Create controller
        HomeController = new HomeController();

        var fakeControllerContext = A.Fake<ControllerContext>();
        var fakeHttpContext = A.Fake<HttpContextBase>();
        var fakePrincipal = A.Fake<IPrincipal>();
        var fakeIdentity = new GenericIdentity("someusername");
     
        HomeController.ControllerContext = fakeControllerContext;
        A.CallTo(() => fakeControllerContext.HttpContext).Returns(fakeHttpContext);
        A.CallTo(() => fakeHttpContext.User).Returns(fakePrincipal);        
        A.CallTo(() => fakePrincipal.Identity).Returns(fakeIdentity);
    };
}

Now when you use HomeController.User property it will point to the fake principal which was provided.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜