Chetan
Chetan

Reputation: 27

How to mock Internal class in Telerik

//Claim class Developed in solution ABC
class Claim
{
    public Claim(string s)
    {
        // progreamm.....
    }
}

//Test Case needs to write in Solution XYZ
[TestClass]
public class ClaimTest
{
    public void myconstructor()
    {
        //Now Question is how to access class Claim here?
    }
}

//Now Question is how to access class Claim constructor in myconstructor //function.

//Hope you get what i need to access using Just Mock in telerik

Upvotes: 0

Views: 420

Answers (2)

Sam
Sam

Reputation: 180

You can use Typemock Isolator for faking internal types. So in your test, you will use:

var fakeInternal = Assembly.Load("ABC").GetType("ABC.Claim");
var fake = Isolate.NonPublic.Fake.Instance(fakeInternal);

And from this point, you can use the Isolate.NonPublic.WhenCalled API for setting the method behavior.

And use the Isolate.Invoke.Method API to invoke the relevant methods.

Upvotes: 1

Stefan Dragnev
Stefan Dragnev

Reputation: 14473

What you do is add the InternalsVisibleTo attribute to the assembly that contains the internal class. It is explained in the JustMock documentation.

[assembly: InternalsVisibleTo("TestAssembly")]
[assembly: InternalsVisibleTo("Telerik.JustMock, PublicKey=0024000004800000940000000602000000240000525341310004000001000100098b1434e598c6" +
"56b22eb59000b0bf73310cb8488a6b63db1d35457f2f939f927414921a769821f371c31a8c1d4b" +
"73f8e934e2a0769de4d874e0a517d3d7b9c36cd0ffcea2142f60974c6eb00801de4543ef7e93f7" +
"9687b040d967bb6bd55ca093711b013967a096d524a9cadf94e3b748ebdae7947ea6de6622eabf" +
"6548448e")]

After that, rebuild the assembly and then you can use the internal types in the test assembly, just as if they were public types.

Upvotes: 2

Related Questions