vendredi 25 mars 2016

How to moq a class with Get() and Set() methods

I can't seem to moq my class that has getter and setter methods (not properties). I need to save the variable somewhere to return it later on.

Signature of my class:

public interface IMyClass
{
    int Get();
    void Set(int i);
}

My real implementation :

  • Set() => Encrypts the int and saves it in a local variable
  • Get() => Decrypts the int from local variable and returns int

How I want the moq to behave :

public class MyClassFake : IMyClass
{
    private int _local;
    public int Get()
    {
        return _local;
    }

    public void Set(int i)
    {
        _local = i;
    }
}

What I've tried and didn't work so far :

        var mockMyClass = new Mock<IMyClass>();
        var number = new int();
        mockMyClass.Setup(x => x.Set(It.IsAny<int>())).Callback<int>(x=> number = x);
        mockMyClass.Setup(x => x.Get()).Returns(number);

Aucun commentaire:

Enregistrer un commentaire