jeudi 17 septembre 2015

Moq setting method return value

I have the below class, and I am trying to test the method AddRecordToQueue.

I am using Moq to mock the result of the the AddToQueue method within the AddRecordToQueue method.

The AddToQueue method returns a boolean, so i am trying to mock the result with a true value

public class Test
{
    private readonly IRabbitMqConnection rabbitMqConnection;

    public Test(IRabbitMqConnection rabbitMqConnection)
    {
        this.rabbitMqConnection = rabbitMqConnection;

    }

    public bool AddRecordToQueue(string messageExchange, object data)
    {
        var jsonified = JsonConvert.SerializeObject(data);
        var customerBuffer = Encoding.UTF8.GetBytes(jsonified);
        var result = this.rabbitMqConnection.AddToQueue(customerBuffer, messageExchange);
        return result;
    }
}

My test class looks like the below.

[TestClass]
public class TestCon
{
    [TestMethod]
    public void MockTest()
    {
        Moq.Mock<IRabbitMqConnection> rabbitConection = new Moq.Mock<IRabbitMqConnection>();

        var draftContactsManager = new Test(rabbitConection.Object);

        rabbitConection.Setup(e => e.AddToQueue(null, string.Empty)).Returns((bool res) => true);

        var result = draftContactsManager.AddRecordToQueue("someExchange", null);

        Assert.IsTrue(result);
    }
}

I cant seem to set the moq result as true. Can anyone advise what I am missing

thanks

Aucun commentaire:

Enregistrer un commentaire