Mocking method returning dynamic return type using Moq

Given the following interface:

public interface IApiHelper
{
   dynamic CallApi(string url);
}

I sent an instance of a Mock<IApiHelper> _apiHelperMock

I am trying to write a test that returns the Success = true property to simulate a JSON result. My setup looks like this:

_apiHelperMock.Setup(o => o.CallApi(It.IsAny<string>())).Returns((dynamic)new { Success = true });

However, when I try to run the test, I get the following error:    Moq.Language.Flow.ISetup 'does not contain a definition for' Returns'

Can someone tell me what I'm doing wrong here?

+4
source share
3 answers

You do not need to specify an anonymous type object dynamic.

Try the following:

_apiHelperMock
    .Setup(o => o.CallApi(It.IsAny<string>()))
    .Returns(new { Success = true });
+1
source

ExpandoObject object.

dynamic userInfo = new ExpandoObject();
dynamic user1 = new ExpandoObject();
user1.title = "aaa";
dynamic user2 = new ExpandoObject();
user2.title = "bbb";
userInfo.groups = new List<ExpandoObject> { user1 , user2 };

var endpointMock = new Mock<IRestEndpointHandler>();
endpointMock.Setup(c => c.RequestJsonDynamicGet(It.IsAny<Uri>())).Returns((object)userInfo);
+2

Dictionary<string,string>:)

0

Source: https://habr.com/ru/post/1621531/


All Articles