Description
When mocking a class which have its constructor calling a method that itself is also mocked (being virtual / abstract), cause the MockEngine _engine field in the generated mock implementation not being set in time when already calling the base() constructor that in turn call the mocked method :
Expected Behavior
The MockEngine _engine field should be set before calling base() constructor
Actual Behavior
The base() constructor is called before MockEngine _engine field have been set.
Steps to Reproduce
public class MyClass
{
public MyClass()
{
A();
}
public virtual void A()
{ }
}
[Test]
public async Task MyTest()
{
var mock = MyClass.Mock();
mock.Object.A();
await Assert.That(mock.A).WasCalled();
}
See in debugging steps that in source generated file sealed class StateTests_MyClassMockImpl file
its constructor
- call the
base() constructor
MyClass constructor call A()
- mocked
StateTests_MyClassMockImpl.A() method is then called
- NullReferenceException is thrown on
_engine field
TUnit Version
1.65.68
.NET Version
.Net 9.0 C#14 preview
Operating System
Windows
IDE / Test Runner
Visual Studio
Error Output / Stack Trace
[Null Reference] Object reference not set to an instance of an object.
Additional Context
No response
IDE-Specific Issue?
Description
When mocking a class which have its constructor calling a method that itself is also mocked (being virtual / abstract), cause the
MockEngine _enginefield in the generated mock implementation not being set in time when already calling thebase()constructor that in turn call the mocked method :Expected Behavior
The
MockEngine _enginefield should be set before callingbase()constructorActual Behavior
The
base()constructor is called beforeMockEngine _enginefield have been set.Steps to Reproduce
See in debugging steps that in source generated
file sealed class StateTests_MyClassMockImplfileits constructor
base()constructorMyClassconstructor callA()StateTests_MyClassMockImpl.A()method is then called_enginefieldTUnit Version
1.65.68
.NET Version
.Net 9.0 C#14 preview
Operating System
Windows
IDE / Test Runner
Visual Studio
Error Output / Stack Trace
[Null Reference] Object reference not set to an instance of an object.Additional Context
No response
IDE-Specific Issue?
dotnet testordotnet run, not just in my IDE