locked
Shim not applied RRS feed

  • Question

  • I just switched from VS2010 to VS2012 (.net 4.0). Now I would like to use MS Fakes. I defined a shim but I doesn't get applied... If I check in Modules my fake assembly is loaded though. I don't see any error trace but my original code (what's supposed to be replaced by a shim) still throws an exception...
    Wednesday, July 3, 2013 2:23 PM

Answers

  • Hello,

    Thank you for your post.

    As far as I know, A shim is used to modify the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies.

    I think that you can learn how to write a test with shims from this article:

    http://msdn.microsoft.com/en-us/library/hh549176.aspx

    As my understanding, you can try to get the text after mocking the StaticManager and then add assertion to check if a DummyText get back.

    If you are shimming a constructor, please refer to ‘Constructor’ section in that article.

    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.

    • Marked as answer by Amanda Zhu Thursday, July 18, 2013 1:34 AM
    Friday, July 5, 2013 2:11 AM
  • Dunken,

    Yes, it is supposed to work. Shims are AppDomain-global and not thread-specific. In other words, a shim applied on one thread will be active on another thread. However, if the StaticManager accesses CssTreeNode on a different thread, it may be possible that by the time that happens, the ShimsContext has already been disposed and the shim has been removed.

    • Marked as answer by Amanda Zhu Thursday, July 18, 2013 1:34 AM
    Wednesday, July 10, 2013 6:29 PM

All replies

  • Would you mind posting the code that demonstrates the problem?
    Wednesday, July 3, 2013 4:07 PM
  • [TestMethod]
    public void TestMethod1()
    {
        // Shims can be used only in a ShimsContext:
        using (ShimsContext.Create())
        {
            // Shim 
            Pvvs.Types.Fakes.ShimCssTreeNode.AllInstances.DisplayNameAssembledGet =
                node => new LangText("DummyText");
    
            // Mock StaticManager
            var staticManager = new StaticManagerMock();
    My static manager internally accesses the property DisplayNameAssembled of a CssTreeNode instance... I would expect to get a DummyText back but instead my original getter is called.
    Thursday, July 4, 2013 6:15 AM
  • Hello,

    Thank you for your post.

    As far as I know, A shim is used to modify the compiled code of your application at run time so that instead of making a specified method call, it runs the shim code that your test provides. Shims can be used to replace calls to assemblies that you cannot modify, such .NET assemblies.

    I think that you can learn how to write a test with shims from this article:

    http://msdn.microsoft.com/en-us/library/hh549176.aspx

    As my understanding, you can try to get the text after mocking the StaticManager and then add assertion to check if a DummyText get back.

    If you are shimming a constructor, please refer to ‘Constructor’ section in that article.

    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.

    • Marked as answer by Amanda Zhu Thursday, July 18, 2013 1:34 AM
    Friday, July 5, 2013 2:11 AM
  • Thank you Amanda!

    I extended my test and it works if access the property right away but it doesn't if a access it internally...

    using (ShimsContext.Create())
    {
        // Shim 
        Pvvs.Types.Fakes.ShimCssTreeNode.AllInstances.DisplayNameAssembledGet =
            node => new LangText("DummyText");
    
        // Mock StaticManager
        var staticManager = new StaticManagerMock();
    
        var test = CssTreeNode.factoryForUnitTest();
        var displayName = test.DisplayNameAssembled; //here I get "DummyText" -> OK
    
        //here I call a complex method which internally uses an instance of CssTreeNode
        //however, this time my shim is NOT being called
        var displayName2 = staticManager.GetSomethingWhatAccessesMyProperty(); //NOT OK!!
    PS: GetSometingWhatAccessesMyProperty internally uses a managed C++ component which in turn accesses DisplayNameAssembled.



    • Edited by Dunken Friday, July 5, 2013 8:54 AM PS added
    Friday, July 5, 2013 6:49 AM
  • Hello,

    I tried to create a scenario to repro your issue but stuck at varstaticManager =newStaticManagerMock(). I did some search and don’t find related information about StaticManagerMock class.

    Whether you are using Mocking framework in unit test?  As far as I know, the MS Fakes framework supplies stubs and shims, whereas NMock, RhinoMocks, and Moq all provide stubs and mocks.

    For more information, please see: http://stackoverflow.com/questions/9677445/mock-framework-vs-ms-fakes-frameworks

    I suggest that you try to define the instance of the managed C++ component and then use it to call the GetSomethingWhatAccessesMyProperty() instead of using StaticManagerMock instance to check the result.

    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, July 8, 2013 4:06 AM
  • Keep in mind that detours live only within a single ShimsContext, defined by the boundaries of the using statement in your test. Please verify that the code accessing the DisplayNameAssembled property is executing within the ShimsContext where it is detoured. If you are still experiencing the problem, we will need a working source code we can use to duplicate the problem.

    Monday, July 8, 2013 4:07 PM
  • Hi guys,

    Thank you for your help.

    I don't use a mocking framework. StaticManagerMock is a dummy class of mine which implements a interface I want to test.

    Everything is within a single ShimsContext but something important I have to mention is that my instance of CssTreeNode is created from another thread! Is this even supposed to work?

    Regards,

    Wednesday, July 10, 2013 8:31 AM
  • Dunken,

    Yes, it is supposed to work. Shims are AppDomain-global and not thread-specific. In other words, a shim applied on one thread will be active on another thread. However, if the StaticManager accesses CssTreeNode on a different thread, it may be possible that by the time that happens, the ShimsContext has already been disposed and the shim has been removed.

    • Marked as answer by Amanda Zhu Thursday, July 18, 2013 1:34 AM
    Wednesday, July 10, 2013 6:29 PM