Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 213
level_zebra Avatar asked Sep 17 '15 14:09

level_zebra


People also ask

What does Moq setup do?

Moq, how it works The idea is to create a concrete implementation of an interface and control how certain methods on that interface responds when called. This will allow us to essentially test all of the paths through code.

How do you mock a method in C#?

Trying to mock a method that is called within another method. // code part public virtual bool hello(string name, int age) { string lastName = GetLastName(); } public virtual string GetLastName() { return "xxx"; } // unit test part Mock<Program> p = new Mock<Program>(); p. Setup(x => x. GetLastName()).

What does Moq callback do?

A powerful capability of Moq is to attach custom code to configured methods and properties' getters and setters. This capability is often referred to as Callbacks.


2 Answers

I think that you need to change the Returns to just return true instead of the lambda. Like this:

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

EDIT:

If this still doesn't work then it is probably due to the parameters not matching. You are passing in "someExchange" but the mock is set up for string.Empty. If you aren't sure what values will be used you could use the It.IsAny method to get around this.

rabbitConection.Setup(e => e.AddToQueue(It.IsAny<byte[]>(), It.IsAny<string>())).Returns(true) 
like image 113
TomDoesCode Avatar answered Sep 22 '22 21:09

TomDoesCode


You need to setup the method with the actual arguments it's invoked. If JsonConvert.SerializeObject(data) returns null, then this is the setup:

rabbitConection.Setup(e => e.AddToQueue(null, "someExchange")).Returns(true) 

Additionally, you can setup the method to return true/false regardless of values of the arguments:

rabbitConection.Setup(e => e.AddToQueue(It.IsAny<byte[]>(), It.IsAny<string>())).Returns(true) 

With the above setup, the method will return true no matter what what you've passed to the method. The previous example will return true only when the method is called with the setuped arguments.

like image 32
Dimitar Tsonev Avatar answered Sep 24 '22 21:09

Dimitar Tsonev