Closed BlacKCaT27 closed 7 years ago
Thanks for the report! Fixed in 1.5.2.
Thanks for the update!
Things are working better but unfortunately I'm still seeing an issue with this.
Previously, the field wasn't being created at all, which was obviously failing compilation. Now, the field is being created, but the corresponding generic type parameter isn't being generated so compilation still fails.
I feel like this may be partially my fault, as my post didn't provide a good example code of what I'm talking about. Allow me to remedy that here. For clarity, I'll use ASP.Net's ILogger generic interface as an example:
If I have a class with this definition:
public class MyClass : IMyClass{
private ILogger<IMyClass> _logger;
public MyClass(ILogger<IMyClass> logger){
_logger= logger;
}
}
This is what I would expect to be generated by the generator:
namespace MyProject.MyNameSpace
{
[TestFixture]
public class MyClassTests
{
private ILogger<IMyClass> _mockLogger;
[SetUp]
public void TestInitialize()
{
_mockLogger = Substitute.For<ILogger<IMyClass>>();
}
[Test]
public void TestMethod1()
{
MyClass myClassUnderTest = this.CreateMyClass();
}
private MyClass CreateMyClass()
{
return new MyClass(
_mockLogger);
}
}
}
However, this is what v1.5.2 of the generator creates:
namespace MyProject.MyNameSpace
{
[TestFixture]
public class MyClassTests
{
private ILogger _mockLogger;
[SetUp]
public void TestInitialize()
{
_mockLogger = Substitute.For<ILogger>();
}
[Test]
public void TestMethod1()
{
MyClass myClassUnderTest = this.CreateMyClass();
}
private MyClass CreateMyClass()
{
return new MyClass(
_mockLogger);
}
}
}
Notice the type of _mockLogger and the type parameter being given to Substitute.For<>(): The type parameter of ILogger is not being carried forward into the generation, but since MyClass needs that more specific type (you can't give just a generic ILogger to something that wants ILogger
Does this make sense? I can open a new issue if you'd prefer.
You'll need to update your custom template to take advantage of the new feature. Change $InterfaceNameBase$ to $InterfaceMockName$ and change $InterfaceName$ to $InterfaceType$ .
Installed product versions
Description
When using constructor injection with objects whose types have generic type parameters, the BoilerPlate Generator fails to create the mock object. The private member variable is not created, the object is missing from the TestInitialize() method, and the Create method uses the text "TODO" in place of the missing variable.
Steps to recreate
Current behavior
See description
Expected behavior
Injected objects utilizing generic type parameters are generated and mocked just like non generic objects.