I’m creating one test to action edit. That’s action return one viewmodel was that create from entity using automapper, but in my test I receve exception because automapper not was initialized. How do I do initialize automapper in my tests?
My test code below:
public void Action_Editar_Deve_Retornar_ProdutoFormModel_Carregado()
{
var produto = FakeDadosProduto.CriarProduto(1);
var produtos = new Mock<IProdutos>();
produtos
.Setup(p => p.Obter(It.IsAny<Guid>()))
.Returns(produto);
var controller = CriarController(produtos.Object);
var viewResult = controller.Editar(Guid.NewGuid());
Assert.AreEqual("produto teste-1", ((ProdutoFormModel)viewResult.Model).Nome);
}
Tks.
In your test setup (or test fixture setup to call the initialization just once) you can call the method that initialize Automapper. Suppose you have a method InitializeAutoMapper in your global.asax, your test fixture setup should be:
Also, you can inject the mapping engine of Automapper in your controllers and fake it in your tests and so you dont need initialize Automapper.