Arrange doens't work with internal members

6 posts, 1 answers
  1. Florian
    Florian avatar
    9 posts
    Member since:
    Jul 2015

    Posted 28 Jul 2015 Link to this post

     I have this code:

    <p>using System;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Telerik.JustMock;
    using Telerik.JustMock.Core;
      
      namespace TestMock1
    {
        [TestClass] public class UnitTest1 {
             
    [TestMethod]
            public void TestMethod1()
            { var mock = Mock.Create<Cls1>(); Mock.Arrange(() => mock.Func1()).Returns(2);
                  mock.Func2();
            }
        }
      public class Cls1 { public Cls1()
            {
      
            }
      internal virtual int Func1()
            { int x = 1; return x;
            }
      internal virtual int Func2()
            { return Func1();
            }
      
        }
      
      
    }​</p>

  2. Florian
    Florian avatar
    9 posts
    Member since:
    Jul 2015

    Posted 28 Jul 2015 in reply to Florian Link to this post

    Sorry, hit Post by mistake before finishing the message. Looks like there is no edit button so I'll just continue here

    The problem is Func1() is actually called, not replaced by the specified Returns(2). It works as expected if I change from internal to public. I couldn't find in documentation that this is limitation of the Free edition, in which case I shd get an error message, like I do with other features supported in Elevated mode only

  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 31 Jul 2015 Link to this post

    Hi Florian,

    Thank you for contacting us. For JustMock Lite to know about internal members, you have to set the InternalsVisibleTo key in the project's AssemblyInfo.cs. This is better explained here and the key you have to add is as follows:
    [assembly: InternalsVisibleTo("Telerik.JustMock, PublicKey=0024000004800000940000000602000000240000525341310004000001000100098b1434e598c6" +
    "56b22eb59000b0bf73310cb8488a6b63db1d35457f2f939f927414921a769821f371c31a8c1d4b" +
    "73f8e934e2a0769de4d874e0a517d3d7b9c36cd0ffcea2142f60974c6eb00801de4543ef7e93f7" +
    "9687b040d967bb6bd55ca093711b013967a096d524a9cadf94e3b748ebdae7947ea6de6622eabf" +
    "6548448e")]

    Further, after adding the above, you will notice that the test execution doesn't enter the Func2 logic at all and thus, the mock.Func2() call returns 0. This is so, due to the fact you have used a complete mock for the Cls1 class. In order to arrange only the Func1 method and keep the original implementation of Func2, you will have to either apply partial mocking, or use Behavior.CallOriginal(). I hope this helps.

    Regards,
    Kaloyan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  5. Florian
    Florian avatar
    9 posts
    Member since:
    Jul 2015

    Posted 04 Aug 2015 in reply to Kaloyan Link to this post

    Thanks a lot.

    One more question. Is this required only for the Lite version? I know that we have to add InternalsVisibleTo attribute for our assemblies but adding it for "Telerik.JustMock" (along with its Public Key)  is required only for Lite version or we have to add it for commercial version too?

  6. Answer
    Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 07 Aug 2015 Link to this post

    Hi Florian,

    The InternalsVisibleTo JustMock entry is needed only when you want to mock internal or non-public members via proxy. In other words, only for the lite version of JustMock, as the commercial will use the profiler for that purpose.

    Regards,
    Kaloyan
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Florian
    Florian avatar
    9 posts
    Member since:
    Jul 2015

    Posted 07 Aug 2015 in reply to Kaloyan Link to this post

    Thanks again
Back to Top
DevCraft R3 2016 release webinar banner