Reputation: 2237
I've started work on Unit Testing in Dot Net Core 2.0. I've written a class to Unit Test controller class of my Web API.
Please find below the code of my Unit Test Class
public class ArticlesControllerTests
{
private ArticlesController _articlesController;
private Mock<IArticleRepository> _articleRepositoryMock = new Mock<IArticleRepository>();
public ArticlesControllerTests()
{
_articlesController = new ArticlesController(_articleRepositoryMock.Object);
}
[ClassInitialize]
public static void Init()
{
AutoMapperInit.Initialize();
}
}
I want to write Init function that will call once and initialize AutoMapper configurations. But [ClassInitialize]
is not there. I've searched online but I was not able to find that attribute.
What should I do to make it work for Dot Net Core 2.0.
Upvotes: 1
Views: 1315
Reputation: 247571
Using a static constructor on the test class should provide the same behavior
public class ArticlesControllerTests {
private ArticlesController _articlesController;
private Mock<IArticleRepository> _articleRepositoryMock = new Mock<IArticleRepository>();
public ArticlesControllerTests() {
_articlesController = new ArticlesController(_articleRepositoryMock.Object);
}
static ArticlesControllerTests() { //<-- static constructor
AutoMapperInit.Initialize();
}
//...
}
the static constructor will call once and initialize AutoMapper configurations
Upvotes: 2