Given the following interface:
public interface IApiHelper
{
dynamic CallApi(string url);
}
I've delclared an instantiated a Mock<IApiHelper> _apiHelperMock
I'm trying to write a test that returns a Success = true property, to mimic a JSON result. My setup looks like this:
_apiHelperMock.Setup(o => o.CallApi(It.IsAny<string>())).Returns((dynamic)new { Success = true });
However I get the following error when trying to run the test: Moq.Language.Flow.ISetup' does not contain a definition for 'Returns'
Can anyone tell me what I'm doing wrong here?
I was able to create an ExpandoObject
and cast it to 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);
You don't have to cast the anonymous type object to dynamic
.
Try this:
_apiHelperMock
.Setup(o => o.CallApi(It.IsAny<string>()))
.Returns(new { Success = true });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With