With R3 2023 (2023.3.1011.155) JustMock introduces a new functionality that might lead to a huge performance drop and event to unexpected failures. Currently, the issue could be suppressed by setting up an environment variable JUSTMOCK_NEWOBJ_INTERCEPTION_ON_OVERWRITE_ENABLED to 0 (the default value is 1), but a more reliable and independent solution should be found.
There are no ReturnsAsync methods for mocking container async methods.
There should be a set of methods to mock async methods of a container similar to regular object mocking.
As a user I should be able to write code like:
container.Arrange<IContainer>(r => r.SomeAsyncMethod("data")).ReturnsAsync("returnValue");
Collection mock (result from ReturnCollection clause) does not support async queries due to the unimplemented IAsyncQueryProvider interface. The issue could be easily reproduced with the following simple test:
[Fact]
public void ShouldReturnEntity()
{
var db = Mock.Create<MyModel.MyModel>();
Mock.Arrange(() => db.SomeEntities).ReturnsCollection(someEntities);
var entity = db.SomeEntities.SingleAsync(x => x.Id == 1);
Assert.NotNull(entity);
}
The outcome is the the following exception:
System.InvalidOperationException The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only providers that implement 'IAsyncQueryProvider' can be used for Entity Framework asynchronous operations.
This article mentions a package that can be used to very easily mock Entity Framework types (mainly DbContext). However, this package only works for Entity Framework 6, and not Entity Framework Core, which my project uses. I would love to see this package updated to greatly simplify unit testing for my project.
I'm specifically looking to mock the following types: IDbContextFactory, my subclass of DbContext, and DbSet. All of this is currently possible of course with the standard JustMock interface but it's a hassle.
We're using this DevOps task in our pipeline, however since yesterday we get the following warning:
##[warning]Task 'Telerik JustMock VSTest v.2' version 2 (JustMockVSTest@2) is dependent on a Node version (6) that is end-of-life. Contact the extension owner for an updated version of the task. Task maintainers should review Node upgrade guidance: https://aka.ms/node-runner-guidance
We're running it using a build agent hosted at the Azure DevOps cloud.
if you use the C# using declaration and have JustMock advanced (elevated) mode enabled, the runtime will throw an InvalidProgramException.
Find below the sample code that demonstrates the issue:
public class TestClass : IDisposable
{
public void Dispose()
{
}
}
[TestClass]
public class Fixture
{
public interface ITest { }
[TestMethod]
public async Task Test()
{
ITest mock = Mock.Create<ITest>();
using TestClass test = new();
}
}
Having the simple class
public class MyClass
{
public event Func<EventArgs, Task>? MyEventAsync;
}
and the test that tries to rise the declared event
[TestMethod]
public void TestMethod1()
{
var mockClass = Mock.Create<MyClass>(Behavior.Strict);
mockClass.MyEventAsync += (args) => Task.CompletedTask;
Mock.Raise(() => mockClass.MyEventAsync += null, EventArgs.Empty);
}
Execution triggers the following error:
Telerik.JustMock.Core.MockException : Event signature System.Threading.Tasks.Task OnMyEventAsync(System.EventArgs) is incompatible with argument types (Castle.Proxies.ExternalMockMixinProxy, System.EventArgs)
which is kind of unexpected since the supplied arguments are matching to the event's signature.
Hi.
i saw this: https://www.telerik.com/forums/how-can-i-mock-multiple-instances-of-a-struct
but still dont understand why JustMock works that way in the first place. why does it union struct mocks by value?
the above solution is only possible when i mock a struct i can change (and then add the id to it) but what about struct's from the framework that i cannot control? Is there a way to tell JustMock not to union mock structs?
Thanks,
Yosi
Provide support for .NET MAUI projects.
Let me explain in details the current situation:
I have Unit Test project using JustMock and running into a problem. It seems like JustMock is using the wrong version number to locate the SDK. let me explain:
The project is set to: <TargetFramework>net7.0</TargetFramework>
But it also has <UseMaui>true</UseMaui> enabled
I am getting the following error:
"Framework: 'Microsoft.Maui.Core', version '7.0.92' (x64) .NET location: C:\Program Files\dotnet No frameworks were found.
That 7.0.92 version is only for the MAUI workload version, not the SDK versions. It seems JustMock uses a workload version as a SDK version.
The issue is about interoperability between JustMock and AutoFixture. The unit test run can be 90% faster if all the fixtures are created before the JustMock arrangements.
Here is a sample code that figures out the issue:
[Fact]
public void Slow
{
var fixture = new Fixture():
var items1 = fixture.Create<List<Item>>();
Nock.Arrange(() => ItemsRepository.GetItems1()).Returns(items1);
var items2 = fixture.Create<List<Item>>();
Nock.Arrange(() => ItemsRepository.GetItems2()).Returns(items2);
var items3 = fixture.Create<List<Item>>();
Nock.Arrange(() => ItemsRepository.GetItems3()).Returns(items3);
var items4 = fixture.Create<List<Item>>();
Nock.Arrange(() => ItemsRepository.GetItems4()).Returns(items4);
}
[Fact]
public void Fast()
{
var fixture = new Fixture():
var items1 = fixture.Create<List<Item>>();
var items2 = fixture.Create<List<Item>>();
var items3 = fixture.Create<List<Item>>();
var items4 = fixture.Create<List<Item>>();
Nock.Arrange(() => ItemsRepository.GetItems1()).Returns(items1);
Nock.Arrange(() => ItemsRepository.GetItems2()).Returns(items2);
Nock.Arrange(() => ItemsRepository.GetItems3()).Returns(items3);
Nock.Arrange(() => ItemsRepository.GetItems4()).Returns(items4);
}
The test duration should not be dependent on the exact implementation.
Considering the sample class
using Azure.Messaging.ServiceBus;
public class Class2
{
private static ServiceBusSender messageToTopicSender;
private static string topicName;
public static void SetRequestTopicClient(string serviceBusConnectionString, string topName)
{
topicName = topName;
ServiceBusClient serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
messageToTopicSender = serviceBusClient.CreateSender(topicName);
}
}
and the following tests
[TestClass]
public class Class1Test
{
[TestInitialize]
public void SetUp()
{
Mock.Arrange(() => Class2.SetRequestTopicClient("", "")).DoNothing();
}
[TestMethod]
public void TestA()
{
Class2.SetRequestTopicClient("", "");
}
}
[TestClass]
public class Class2Test
{
static ServiceBusClient serviceBusClient = Mock.Create<ServiceBusClient>();
ServiceBusSender messageToTopicSender = Mock.Create<ServiceBusSender>();
[TestInitialize]
public void SetUp()
{
Mock.Arrange(() => new ServiceBusClient("conStr")).Returns(serviceBusClient);
Mock.Arrange(() => serviceBusClient.CreateSender("myTopic")).Returns(messageToTopicSender);
}
[TestMethod]
public void TestB()
{
Class2.SetRequestTopicClient("conStr", "myTopic");
}
}
Executing the tests in order TestA -> TestB results in failed TestB, but changing the order or runining them standalone succeeds.The outcome of the tests should not be dependent of the execution order.
A unit test run against a simple class using EntityFramework never completes, here's the code:
public class DbContext1 : DbContext
{
public DbContext1(string connectionString)
{
}
}
public class Program
{
private static readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
public async Task Run()
{
await _lock.WaitAsync();
try
{
await InsertDbRow();
}
finally
{
_lock.Release();
}
}
private static async Task InsertDbRow()
{
await RetryWrapperAsync(async () =>
{
using DbContext1 dbContext = new DbContext1("con str");
await dbContext.SaveChangesAsync();
});
}
public static async Task RetryWrapperAsync(Func<Task> operation)
{
for (int i = 0; i < 3; i++)
{
try
{
await operation();
break;
}
catch (Exception)
{
await Task.Delay(100);
}
};
}
}
[TestClass]
public class ProgramTest
{
private readonly DbContext1 mockContext1 = Mock.Create<DbContext1>();
[TestInitialize]
public void SetUp()
{
Mock.Arrange(() => new DbContext1("con str")).Returns(mockContext1);
}
[TestMethod]
public async Task TestMethod()
{
// Arrange
Program program = new Program();
// Act
await program.Run(); // <-- at this point the test hangs
}
}
Adding do-nothing arrangement on mockContext.SaveChanges fixes the hang, but the expectation is that mock will handle this case by default and there is no need to be explicitly arranged.
Allow the developer to create custom behaviors and use them in an arrangement. Something like the following:
Mock.Arrange(() => foo.CalcData(Arg.AnyInt, Arg.AnyInt), new IBehavior[]
{
new LogInvocation(),
new ReturnBaseOrDefaultValue(),
});