This is a migrated thread and some comments may be shown as answers.

Arrange doens't work with internal members

5 Answers 20 Views
JustMock Free Edition
This is a migrated thread and some comments may be shown as answers.
Florian
Top achievements
Rank 1
Florian asked on 28 Jul 2015, 05:20 PM

 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>

5 Answers, 1 is accepted

Sort by
0
Florian
Top achievements
Rank 1
answered on 28 Jul 2015, 05:25 PM

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

0
Kaloyan
Telerik team
answered on 31 Jul 2015, 02:49 PM
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
0
Florian
Top achievements
Rank 1
answered on 04 Aug 2015, 02:39 PM

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?

0
Accepted
Kaloyan
Telerik team
answered on 07 Aug 2015, 08:18 AM
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
0
Florian
Top achievements
Rank 1
answered on 07 Aug 2015, 07:30 PM
Thanks again
Tags
JustMock Free Edition
Asked by
Florian
Top achievements
Rank 1
Answers by
Florian
Top achievements
Rank 1
Kaloyan
Telerik team
Share this question
or