i have been trying to do some mocking with RestClient, RestResponse
and RestRequest with Moq.
unfortunately i could only mock RestClient and couldn't mock
RestResponse and RestRequest.
if these are changed to interface i can mock.
var mockClient = new Mock<RestClient>();
mockClient.Setup(c => c.BaseUrl).Returns("https://
graph.facebook.com");
the above test fails: System.NotSupportedException: Invalid setup on a
non-virtual (overridable in VB) member: c => c.BaseUrl
but if i change to IRestClient use the interface then it works.
var mockClient = new Mock<IRestClient>();
mockClient.Setup(c => c.BaseUrl).Returns("https://
graph.facebook.com");
so to mock the client, i used the interface IRestClient, but
RestResponse and RestRequest doesn't derive from an interface,
so to solve this i create a fake class deriving from RestRequest.
public class FakeRestRequest : RestRequest
{
public new virtual string Resource { get { return
base.Resource; } set { base.Resource = value; } }
}
now when i do
var mockRequest = new Mock<FakeRestRequest>();
mockRequest.Setup(r => r.Resource).Returns("/me");
it works.
from this stack overflow post:
http://stackoverflow.com/questions/1015315/how-do-you-mock-class-with-readonly-property
"The only mocking engine I know of that allows altering non-virtual
methods on classes and sealed classes is Typemock."
which one should be preferable, RestSharp implements the interface
called IRestRequest and IRestResponse or
we should create our own Fake classes like FakeRestRequest?