Mocking FormsAuthentication

2 posts, 0 answers
  1. Mike
    Mike avatar
    18 posts
    Member since:
    Mar 2008

    Posted 28 Jan 2013 Link to this post

    Is it possible to mock the static FormsAuthentication?  I would like to check that the SignIn and SignOut methods are called in an MVC LoginController.

    I have a mocked LoginController, lc, and the following code:

                Mock.Arrange(() => FormsAuthentication.SignOut()).OccursOnce();
                var result = lc.Logoff();
                Mock.Assert(() => FormsAuthentication.SignOut());

    If I run this under debug it passes but if I just run it I get:
    Test Name:  LogoffShouldCallFormsAuthenticationSignOut
    Test FullName:  OfDisplays.WebTests.SecurityTests.LogoffShouldCallFormsAuthenticationSignOut
    Test Source:    c:\SoftDev\Projects\OFDisplays\OfDisplays.Web\OfDisplays.WebTests\SecurityTests.cs : line 83
    Test Outcome:   Failed
    Test Duration:  0:00:01.9634171
    Result Message:
    Test method OfDisplays.WebTests.SecurityTests.LogoffShouldCallFormsAuthenticationSignOut threw exception:
    System.IO.FileNotFoundException: Could not load file or assembly 'Telerik.CodeWeaver.Api, Version=, Culture=neutral, PublicKeyToken=87210992829a189d' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.
    To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
    Note: There is some performance penalty associated with assembly bind failure logging.
    To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].
    Result StackTrace: 
    at System.Web.Security.FormsAuthentication.SignOut()
       at OfDisplays.Web.Controllers.LoginController.Logoff() in c:\SoftDev\Projects\OFDisplays\OfDisplays.Web\OfDisplays.Web\Controllers\LoginController.cs:line 57
       at OfDisplays.WebTests.SecurityTests.LogoffShouldCallFormsAuthenticationSignOut() in c:\SoftDev\Projects\OFDisplays\OfDisplays.Web\OfDisplays.WebTests\SecurityTests.cs:line 90

  2. Kaloyan
    Kaloyan avatar
    880 posts

    Posted 30 Jan 2013 Link to this post

    Hello Mike,

    Thank you for the detailed feedback.

    I tried reproducing the issue following exactly the same approach as your test method and it worked as expected.

    In order to assist you further, I would suggest you to send us an example project, reproducing the matter. However, if this is not doable in your case, please provide us some information about the environment you have:
    • What Visual Studio is being used
    • Which version of MVC you are writing the tests against

    Thank you for the collaboration in advance.

    Kind regards,
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top