locked
Microsoft Fake Framework / Code first / Repository / SimpleMembership RRS feed

  • Question

  • Hi, I want to unit testing my application with the new Microsoft Fake Framework and I got some problem with it. I'm new to unit testing so may be I don't understand the pattern like I should... 

    So here we go! My application have a repository structure + one generic repo for all get, insert, update etc. I use Code first with SimpleMembership. It's a C# and MVC4 application.

    I got two problem!

    First one, I don't know how to use the Fake Framework with my repositories and DBContext 

    See the screenshot

    My second problem  appear every time I call ShimWebSecurity... I get this error : 

    Result Message:
    Test method VIA.Tests.Controllers.AccountControllerTest.LogOff threw exception: 
    System.Security.VerificationException: Operation could destabilize the runtime.
    Result StackTrace:
    at WebMatrix.WebData.WebSecurity.Logout()
       at VIA.Controllers.AccountController.LogOff() in c:\Users\...\AccountController.cs:line 160
       at VIA.Tests.Controllers.AccountControllerTest.LogOff() in c:\Users\...\VIA.Tests\Controllers\AccountControllerTest.cs:line 52

    The code is simple :

            [TestMethod]
            public void LogOff()
            {
                var AccountController = new AccountController();
                var WebSecuritySignOutCalled = false;
                RedirectToRouteResult RedirectToRouteResult;

                //Scope the detours we're creating
                using (ShimsContext.Create())
                {
                    ShimWebSecurity.Logout = () =>
                    {
                        //Set a boolean to identify that we actually got here
                        WebSecuritySignOutCalled = true;
                    };
                    var test = AccountController.LogOff();
                    RedirectToRouteResult = AccountController.LogOff() as RedirectToRouteResult;
                    Assert.AreEqual(true, WebSecuritySignOutCalled);
                }

                Assert.IsNotNull(RedirectToRouteResult);
                Assert.AreEqual("Index", RedirectToRouteResult.RouteValues["Action"]);
                Assert.AreEqual("Home", RedirectToRouteResult.RouteValues["controller"]);
            }


    I really need your help or a very detail example with a repository structure & SimpleMembership. I really don't know how can I test my code with Fake Framework without your help. 

    Thank you very much for your time,

    Dave

    Monday, February 18, 2013 12:47 AM

Answers

All replies

  • No buddy cant help me?? Please, I hope Im not the only one on earth with that kind of problem. Any suggestions?
    Monday, February 18, 2013 3:31 AM
  • Hi,

    I am trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.

    Best regards,


    Amanda Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, February 18, 2013 4:44 AM
  • Hello Dave,

    This VerificationException issue sounds like a known issue, please check the following KB:

    http://support.microsoft.com/kb/2748645

    Thanks & Regards,

    Jian-Wei Yu [MSFT]

    Microsoft Online Community Support

    --------------------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Tuesday, February 19, 2013 5:51 AM
  • I'm sorry but it is not the solution. The problem is still there after installing the path and restart my computer. 

    You could easily reproduce the problem by creating a new MVC4 application with unit test. Import using WebMatrix.WebData, fake it with Microsoft Fake framework and test the log off action from AccountController. That only take five minutes to reproduce the problem. 

    Please let me know if a solution exist out there for that bug.

    Dave

    Wednesday, February 20, 2013 3:29 PM
  • Hello Dave,

    I can reproduce this issue, and our dev team is now working this issue and provide a walk around:

    http://connect.microsoft.com/VisualStudio/feedback/details/740778/verificationexception-when-faking-mvc4-and-instantiating-controller-in-unit-test

    Thanks & Regards,

    Jian-Wei Yu [MSFT]

    Microsoft Online Community Support


    --------------------------------------------------------------------------------

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Thursday, February 21, 2013 3:51 AM