Moq in c# methods of different classes

Asked

Viewed 71 times

1

Good night.

I’m new to unit testing. I’d like to ask for help to resolve a question.

I have the following method of a class that calls the method of another class:

class public Teste: ITeste
{
   private readOnly ITeste _teste;

   public Teste( ITeste _trans)
   {
      this._teste = _trans;
   }

   public List<Carteira> ObterFila(TimeSpan horaCorte)
   {
      List<Carteira> lstCarteiraInfo = new List<Carteira>();
      lstCarteiraInfo = new DataAcces.Transacao.ObterFilaAlerta(horaCorte);
      return lstCarteiraInfo;
   }
}

I would like to test this class by mocking the Get Alert method that is within the class library, Transacao class, and Dataacces layer. (Transaction class has Itransacao interface)

I tried the following code and was unsuccessful:

[TestClass]
    public class TesteMoq
    {
        private Mock<DataAcces.Interface.ITransacao> _mock;
        private Mock<Business.Interface.ITeste> _mockB;

    [TestMethod]
    public void Valida()
    {
        _mock = new Mock<DataAcces.Interface.ITransacao>();
        _mockB = new Mock<Business.Interface.ITeste>();

        Lista<Carteira> lista = new Lista<Carteira>;

        _mock.Setup(x => x.ObterFilaAlerta(It.IsAny<TimeSpan>())).Returns(lista);
        Business.Teste teste = new Business.Teste(_mock.Object);
        var resultado = teste.ObterAlerta();
        var resultadoEsperado = new Lista<Carteira>;

       Assert.AreEqual(resultado, resultadoEsperado);
    }
}

When Pure this test the Get Queue method call is not being mocked and the implementation is executed.

Could you help me with a similar example?

  • pq your class injects the interface itself? do not understand

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.