Controller class:
private readonly DbSettings _docDbSettings;
public CoursesController(IOptions<DbSettings> docDbSettings)
{
if (docDbSettings == null) throw new ArgumentNullException(nameof(docDbSettings));
_docDbSettings = docDbSettings.Value;
}
Controller Tests class:
public class CoursesControllerTests
{
private readonly IFixture _fixture;
private readonly CoursesController _coursesController;
private readonly DbSettings _docDbSettings;
public CoursesControllerTests()
{
_fixture = new Fixture().Customize(new AutoFakeItEasyCustomization());
// Need help here.
_docDbSettings = _fixture.Create<IOptions<DbSettings>>();
}
}
Error:
Cannot implicitly convert type 'Microsoft.Extensions.Options.IOptions<Infrastructure.DbSettings>' to 'Infrastructure.DbSettings'
Any clues!
Thanks in advance.
It seems you're trying to assign an object of type IOptions<DbSettings>
returned by _fixture.Create<IOptions<DbSettings>>()
to a variable of type DbSettings
when those types aren't compatible.
You can either change the type of _docDbSettings
to IOptions<DbSettings>
or create a DbSettings
object with AutoFixture by saying:
_docDbSettings = _fixture.Create<DbSettings>();
By the way, it's good to know that AutoFixture can work as an auto-mocking container, meaning that you can ask it to create an instance of your CourseController
and AutoFixture will make sure to provide arguments for all of the constructor dependencies, in this case IOptions<DbSettings>
:
var systemUnderTest = _fixture.Create<CourseController>();
You can read more about how to use this pattern with AutoFixture in this article by Mark Seemann.