Telerik® JustMock™ by Progress

With JustMock you can mock internal types with InternalsVisibleToAttribute the same way you mock public types. Without InternalsVisibleToAttribute you are forced to mock as you mock non-public members.

In the further examples, we will use the following sample classes to test:

Mock Internal Type Via Proxy

Provided that the InternalsVisibleTo attribute is included in the AssemblyInfo.cs, let's consider the following example:

Here we verify that the mock is created and its constructor is called. Thus, foo.Builder will be assigned.

The key to be used in the InternalsVisibleTo attribute is as follows:

  • For projects different from Silverlight:

    C# Copy imageCopy
    [assembly: InternalsVisibleTo("Telerik.JustMock, PublicKey=0024000004800000940000000602000000240000525341310004000001000100098b1434e598c6" +
    "56b22eb59000b0bf73310cb8488a6b63db1d35457f2f939f927414921a769821f371c31a8c1d4b" +
    "73f8e934e2a0769de4d874e0a517d3d7b9c36cd0ffcea2142f60974c6eb00801de4543ef7e93f7" +
    "9687b040d967bb6bd55ca093711b013967a096d524a9cadf94e3b748ebdae7947ea6de6622eabf" +
  • For Silverlight projects:

    C# Copy imageCopy
    [assembly: InternalsVisibleTo("Telerik.JustMock")]

Create Mock With Internal Constructor

You can mock class that exposes internal constructor in the same way you mock public types.

In the example above, we mock the internal constructor of FooInternalCtor class. When foo is created the internal constructor will be called with "hello" as argument and the Name property will be assigned "hello" respectively. We verify that by calling foo.Name in the assertion phase of the AAA pattern.

Mock An Interface Member, Privately Implemented In Base Class

Here is an interface with a single member in it and a class which is inheriting that interface.

The derived class Bar will be our object to mock, in the example below.

We have mocked the Bar class. Then we make arranges about the interface member, Provider and we act. Finally we can assert that the test behavior is as expected.

See Also

Other Resources