JustMock-hero
Support .NET 5, .NET 6 preview & .NET Core

Telerik JustMock

The fastest, most flexible and complete mocking tool
for crafting unit tests.

Download Free Trial
trust-radius-badge

Telerik Earns Multiple TrustRadius Best of Development 2021 awards

Telerik has won Best Feature Set and Best Customer Support. The award is based on both quantitative and qualitative feedback provided in user reviews with regards to customer support, feature set and usability.

big_wave_dblue_hero

Explore Some of Our Amazing Features

ninja-peeking
[TestMethod]
public void ShouldMockGenericClass()
{
    int expectedValue = 1;

    // Arrange
    // Creating a mock instance of the "FooGeneric<T>" class.
    var foo = Mock.Create<FooGeneric<int>>();

    // When the virtual foo.Get() is called with any integer as an argument, it should return expectedValue.
    Mock.Arrange(() => foo.Get(Arg.IsAny<int>())).Returns(expectedValue);

    // Act
    int actualValue = foo.Get(0);

    // Assert
    Assert.AreEqual(expectedValue, actualValue);
}
                        
<TestMethod> _
Public Sub ShouldMockGenericClass()
    Dim expectedValue As Integer = 1

    ' Arrange
    ' Creating a mock instance of the "FooGeneric<T>" class.
    Dim foo = Mock.Create(Of FooGeneric(Of Integer))()

    ' When the virtual foo.Get() is called with any integer as an argument, it should return expectedValue.
    Mock.Arrange(Function() foo.[Get](Arg.IsAny(Of Integer)())).Returns(expectedValue)

    ' Act
    Dim actualValue As Integer = foo.[Get](0)

    ' Assert
    Assert.AreEqual(expectedValue, actualValue)
End Sub
                        
[TestMethod]
public void ShouldAssertWithCustomSelect()
{
    var simpleDataInstance = new SimpleData();

    // Arrange - When simpleDataInstance.Products_GET is called, it should return expected collection.
    Mock.Arrange(() => simpleDataInstance.Products).ReturnsCollection(ReturnExpextedCollectionOfProducts());

    // Act - Applying a LINQ query for simpleDataMock.Products.
    var actual = (from p in simpleDataInstance.Products
                    where p.UnitsInStock == 50
                    select p.ProductID).SingleOrDefault();

    // Assert
    Assert.AreEqual(2, actual);
}
                        
<TestMethod> _
Public Sub ShouldAssertWithCustomSelect()
    Dim simpleDataInstance = New SimpleData()

    ' Arrange - When simpleDataInstance.Products_GET is called, it should return expected collection.
    Mock.Arrange(Function() simpleDataInstance.Products).ReturnsCollection(ReturnExpextedCollectionOfProducts())

    ' Act - Applying a LINQ query for simpleDataMock.Products.
    Dim actual = (From p In simpleDataInstance.Products _
                    Where p.UnitsInStock = 50 _
                    Select p.ProductID).SingleOrDefault()

    ' Assert
    Assert.AreEqual(2, actual)
End Sub
                        
[TestMethod]
public void DoPublic_OnExecute_ShouldCallDoPrivate()
{
    var isCalled = false;
    
    Foo foo = new Foo();
    
    // Arrange - When the non-public method DoPrivate() is called from the foo instance, 
    // it should set isCalled to true instead of executing its original logic.
    Mock.NonPublic.Arrange(foo, "DoPrivate").DoInstead(() => isCalled = true);
    
    // Act
    foo.DoPublic(); // DoPublic() should call DoPrivate().
    
    // Assert
    Assert.IsTrue(isCalled);
}
                        
<TestMethod>
Public Sub DoPublic_OnExecute_ShouldCallDoPrivate()
    Dim isCalled = False

    Dim foo As New Foo()

    ' Arrange - When the non-public method DoPrivate() is called from the foo instance, 
    ' it should set isCalled to true instead of executing its original logic.
    Mock.NonPublic.Arrange(foo, "DoPrivate").DoInstead(Function() InlineAssignHelper(isCalled, True))

    ' Act - DoPublic() should call DoPrivate().
    foo.DoPublic()

    ' Assert
    Assert.IsTrue(isCalled)
End Sub
                        
[TestMethod]
public void ShouldFakeStaticPropertyGet()
{
    bool isCalled = false;
    var expected = 1;

    // Arrange
    // Getting the static instance(Foo) ready for mocking disregarding the constructor and applying strict behavior.
    // If we don't mock the constructor, a NotImplementedException will be thrown.
    Mock.SetupStatic(typeof(Foo), Behavior.Strict, StaticConstructor.Mocked);

    // When the static(Foo.FooProp_GET) property is called, 
    // it should assign true to isCalled and return expected instead.
    Mock.Arrange(() => Foo.FooProp).DoInstead(() => { isCalled = true; }).Returns(expected);

    // Act
    var actual = Foo.FooProp;

    // Assert
    Assert.AreEqual(expected, actual);
    Assert.IsTrue(isCalled);
}

                        
<TestMethod>
Public Sub ShouldFakeStaticPropertyGet()
    Dim isCalled As Boolean = False
    Dim expected = 1

    ' Arrange
    ' Getting the static instance(Foo) ready for mocking disregarding the constructor and applying strict behavior.
    ' If we don't mock the constructor, a NotImplementedException will be thrown.
    Mock.SetupStatic(GetType(Foo), Behavior.[Strict], StaticConstructor.Mocked)

    ' When the static(Foo.FooProp_GET) property is called, 
    ' it should assign true to isCalled And return expected instead.
    Mock.Arrange(Function() Foo.FooProp).DoInstead(Sub() isCalled = True).Returns(expected)

    ' Act
    Dim actual = Foo.FooProp

    ' Assert
    Assert.AreEqual(expected, actual)
    Assert.IsTrue(isCalled)
End Sub

                        
[TestMethod]
public void ShouldApplyIgnoreInstanceToVirtual()
{
    var expected = 10;

    // Arrange
    // Creating a mocked instance of the "Calculus" class.
    // This mock will also be used as a fake of a given type for applying a future instance expectation to that type.
    var calculus = Mock.Create<Calculus>();

    // When calculus.Sum() is called, it should return expected.
    // Also, this expectations will apply for all newly created instances of the Calculus class 
    // during the test.
    Mock.Arrange(() => calculus.Sum()).IgnoreInstance().Returns(expected);

    // Act
    var valueFromExistingInstance = calculus.Sum();
    var valueFromNewInstance = new Calculus().Sum();

    // Assert
    Assert.AreEqual(expected, valueFromExistingInstance); // Verifying the value from an already existing instance.
    Assert.AreEqual(expected, valueFromNewInstance); // Verifying the value from a newly created instance.
}

                        
<TestMethod>
Public Sub ShouldApplyIgnoreInstanceToVirtual()
    Dim expected = 10

    ' Arrange
    ' Creating a mocked instance of the "Calculus" class.
    ' This mock will also be used as a fake of a given type for applying a future instance expectation to that type.
    Dim calculus = Mock.Create(Of Calculus)()

    ' When calculus.Sum() is called, it should return expected.
    ' Also, this expectations will apply for all newly created instances of the Calculus class 
    ' during the test.
    Mock.Arrange(Function() calculus.Sum()).IgnoreInstance().Returns(expected)

    ' Act
    Dim valueFromExistingInstance = calculus.Sum()
    Dim valueFromNewInstance = New Calculus().Sum()

    ' Assert
    Assert.AreEqual(expected, valueFromExistingInstance) ' Verifying the value from an already existing instance.
    Assert.AreEqual(expected, valueFromNewInstance) ' Verifying the value from a newly created instance.
End Sub

                        
[TestMethod]
public void ShouldAssertCustomValueForDateTimeNow()
{
    var expected = new DateTime(1900, 4, 12);

    // Arrange - Here we arrange, when DateTime.Now is called it should return expected DateTime.
    Mock.Arrange(() => DateTime.Now).Returns(expected);

    // Act
    var now = DateTime.Now;

    // Assert - the date is indeed what was arranged.
    Assert.AreEqual(expected.Year, now.Year);
    Assert.AreEqual(expected.Month, now.Month);
    Assert.AreEqual(expected.Day, now.Day);
}

                        
<TestMethod>
Public Sub ShouldAssertCustomValueForDateTimeNow()
    Dim expected = New DateTime(1900, 4, 12)

    ' Arrange - Here we arrange, when DateTime.Now is called it should return expected DateTime.
    Mock.Arrange(Function() DateTime.Now).Returns(expected)

    ' Act
    Dim now = DateTime.Now

    ' Assert - the date is indeed what was arranged.
    Assert.AreEqual(expected.Year, now.Year)
    Assert.AreEqual(expected.Month, now.Month)
    Assert.AreEqual(expected.Day, now.Day)
End Sub

                        
[TestMethod]
public void ShouldReturnFakeDbSet_WhenDbContextPropertyIsCalled()
{
    // Arrange
    NerdDinners nerdDinners = new NerdDinners();

    // When the DBSet property nerdDinners.Dinners is called, it should return a predefined fake collection containing 3 items.
    Mock.Arrange(() => nerdDinners.Dinners).ReturnsCollection(FakeDinners());

    // Act - We call the nerdDinners.Dinners and search for a dinner with a certain DinnerID.
    var actualQuery = from d in nerdDinners.Dinners
                        where d.DinnerID == 1
                        select d;

    // Assert - We assert that the nerdDinners.Dinners collection will actually return a collection with 3 items.
    Assert.AreEqual(3, nerdDinners.Dinners.Count());
    // We assert that there is only one item in our collection that has DinnerID equal to one.
    Assert.AreEqual(1, actualQuery.Count());
    Assert.AreEqual(1, actualQuery.First().DinnerID);
}
                        
<TestMethod>
Public Sub ShouldReturnFakeDbSet_WhenDbContextPropertyIsCalled()
    ' Arrange
    Dim nerdDinners As New NerdDinners()

    ' When the DBSet property nerdDinners.Dinners is called, it should return a predefined fake collection containing 3 items.
    Mock.Arrange(Function() nerdDinners.Dinners).ReturnsCollection(FakeDinners())

    ' Act - We call the nerdDinners.Dinners and search for a dinner with a certain DinnerID.
    Dim actualQuery = From d In nerdDinners.Dinners Where d.DinnerID = 1D

    ' Assert - We assert that the nerdDinners.Dinners collection will actually return a collection with 3 items.
    Assert.AreEqual(3, nerdDinners.Dinners.Count())
    ' We assert that there is only one item in our collection that has DinnerID equal to one.
    Assert.AreEqual(1, actualQuery.Count())
    Assert.AreEqual(1, actualQuery.First().DinnerID)
End Sub

                        
Coppied!

Why Choose JustMock

The Full List of JustMock Features

See It in Action

Download Free Trial
ninja-v1-opt

Upgrade to Telerik DevCraft Complete

Be Ready for Any Project & Technology

Save up to 50% in development time by getting 1,250+ .NET and JavaScript UI components for building web, desktop and mobile apps.

Get the Best Value for Money

Enjoy mocking tool for unit testing along with 15 products for .NET and JavaScript apps development and embedded reporting while saving up to 90% from the upgrade price.

What's New

Next-Level Performance Optimization

Next-Level Performance Optimization

The Telerik JustMock Profiler now works on demand. (*Beta version)

Support for .NET 6 Official Release

Support for .NET 6 Official Release

Telerik JustMock ships support for .NET 6 official release.

Support for C# 10

Support for C# 10

Telerik JustMock adds support for C# 10.

Industry-Leading Support

Expert and Timely Support

Get answers to your questions directly from the developers who build this UI suite, even during your trial period.

Contact support

Need Evaluation Help?

If you are not a developer or don't have time to evaluate our product, send us your project requirements. We will evaluate your required features and let you know how our products fit your needs.

Send us your project requirements
Background-NextSteps

Next Steps

See Plans & Pricing

Purchase individual products or any of the bundles

Download Free Trial

With dedicated technical support.