New to Telerik JustMock? Download free 30-day trial

Mock Internal Types Via Proxy

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:

internal class FooInternal 
{ 
    internal FooInternal() 
    { 
        builder = new StringBuilder(); 
    } 
 
    public StringBuilder Builder 
    { 
        get 
        { 
            return builder; 
        } 
    } 
 
    private StringBuilder builder; 
} 
 
public class FooInternalCtor 
{ 
    internal FooInternalCtor(string name) 
    { 
        this.name = name; 
    } 
 
    public string Name { get { return name; } } 
 
    private string name; 
} 

Mock Internal Type Via Proxy

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

[TestMethod] 
public void ShouldMockInternalTypeViaProxy() 
{ 
    // Arrange 
    var foo = Mock.Create<FooInternal>(Behavior.CallOriginal); 
 
    // Assert 
    Assert.IsNotNull(foo.Builder); 
} 

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: InternalMocking#Key
  • For Silverlight projects: InternalMocking#KeySilverlight

Create Mock With Internal Constructor

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

[TestMethod] 
public void ShouldCreateMockWithInternalCtor() 
{ 
    // Arrange 
    var expected = "hello"; 
 
    var foo = Mock.Create<FooInternalCtor>(x => 
    { 
        x.CallConstructor(() => new FooInternalCtor(expected)); 
        x.SetBehavior(Behavior.CallOriginal); 
    }); 
 
    // Assert 
    Assert.AreEqual(foo.Name, expected); 
} 

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.

public interface IManager 
{ 
    object Provider { get; } 
} 
 
public class FooBase : IManager 
{ 
    object IManager.Provider 
    { 
        get { throw new NotImplementedException(); } 
    } 
} 
 
public class Bar : FooBase 
{ 
} 

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

[TestMethod] 
public void ShouldMockAnInterfaceMemberPrivatelyImplementedInBaseClass() 
{ 
    // Arrange 
    var bar = Mock.Create<Bar>(); 
 
    string expected = "dummy"; 
 
    Mock.Arrange(() => ((IManager)bar).Provider).Returns("dummy"); 
 
    // Act 
    var actual = ((IManager)bar).Provider; 
 
    // Assert 
    Assert.AreEqual(expected, actual); 
} 

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.