background

Telerik JustMock

Mock Generics

  • JustMock makes it easy to mock a generic class as easy as mocking a non-generic one.
  • Part of the fastest, most flexible and complete mocking tool for crafting unit tests.
  • Our award-winning support team is available to assist you with any issues.
Mock everything
  • Mocking Generics Overview

    Generics introduce the concept of type parameters to .NET. Type parameters make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code.
    Generics are a pillar of modern software design, often used as method parameters and as such represent a dependency to that method.
  • Mocking Generic Class

    JustMock makes it easy to mock a generic class as easy as mocking a non-generic one. The construction is the same.

    var foo = Mock.Create<FooGeneric<int>>();
  • Mocking Generic Method in Non-Generic Class

    Mocking of generic methods with JustMock is as easy as mocking a non-generic method.

    [TestMethod]
    public void ShouldMockGenericMethodInNonGenericClass()
    {
        //Arrange
        var foo = Mock.Create<Foo>();
        Mock.Arrange(() => foo.Get<int, int>(1)).Returns(10);
         
        // Act
        var actual = foo.Get<int, int>(1);
      
        //Assert
        Assert.AreEqual(10, actual);
    }

    Mock of generics documentation 
Next Steps