2
How do I mock any instance of a class? I would like to do this so I don’t have to mock an object and have to put it inside a class. Example:
[TestFixture]
public class TokenTest
{
GeradorDeToken target;
[Test]
public void GeraTokenComSucesso()
{
int[] array = { 19, 28, 37, 46, 55 };
var mockGeradorDeArray = new Mock<GeradorDeArrayParaToken>();
mockGeradorDeArray.SetupAny(g => g.GeraArray()).Returns(array);
string tokenEsperado = "1E7C6XB9F8A";
token = new GeradorDeToken();
Assert.That(target.GeraToken(), Is.EqualTo(tokenEsperado));
}
}
public class GeradorDeToken
{
private int[] arrayNumeros;
public GeradorDeToken()
{
this.arrayNumeros = new GeradorDeArrayParaToken().GeraArray();
}
public string GeraToken()
{
//Criação do token baseado no arrayNumeros
return "";
}
}
public class GeradorDeArrayParaToken
{
public virtual int[] GeraArray()
{
int[] array = new int[5];
//Gero numeros randomicos
return array;
}
}
I illustrated creating the fictitious method Setupany (does not exist in the Moq library). What is the correct way that this framework solves this question?