I'd like to unit-test the following function with NUnit and Rhino Mocks. The function uses the given parameters to create a RestSharp.RestRequest
and give the request to the dataAccessApi
:
public void CopyToUserSession(string uri, string sourcePath)
{
RestRequest request = new RestRequest(uri, Method.POST);
request.AddParameter("source-path", sourcePath);
dataAccessApi.Request<object>(request, restExecution.Get);
}
This is the testing Class:
public void CopyToUserSession_ValidUriAndParameter_CallDataAccessRequest()
{
// Arrange
var dataAccessApi = MockRepository.GenerateMock<IDataAccessApi>();
var restExecution = MockRepository.GenerateMock<IRestExecution>();
var sinkNodeResource = new SinkNodeResource(dataAccessApi, restExecution);
string uri = "http://SomeUri.com";
string sourcePath = "Some Source Path";
RestRequest request = new RestRequest(uri, Method.POST);
request.AddParameter("source-path", sourcePath);
// Act
sinkNodeResource.CopyToUserSession(uri, sourcePath);
// Assert
dataAccessApi.AssertWasCalled(x => x.Request<object>(request, restExecution.Get));
}
The IDataAccessApi...:
public interface IDataAccessApi
{
void Request<T>(RestRequest request, Action<T> callbackAction) where T : new();
}
... and the IRestExecution:
public interface IRestExecution
{
void Get(object o);
void Put(object o);
void Post(object o);
void Delete(object o);
}
The test worked with easier functions to call (like x.Test("Some String")
) but does not with the Action new RestExecution().Get
- how do i unit-test such calls?
Thank you in advance for your help!
Edit: Changed the Code regarding @rich.okelly comment.
Edit: Added the code for the interfaces
Try the following:
// arrange
var dataAccessApi = MockRepository.GenerateMock<IDataAccessApi>();
var restExecution = MockRepository.GenerateMock<IRestExecution>();
var sinkNodeResource = new SinkNodeResource(dataAccessApi, restExecution);
string uri = "http://SomeUri.com";
string sourcePath = "Some Source Path";
// act
sinkNodeResource.CopyToUserSession(uri, sourcePath);
// assert
dataAccessApi.AssertWasCalled(
x => x.Request<object>(
Arg<RestRequest>.Matches(
y => y.Method == Method.POST &&
y.Resource == uri &&
y.Parameters.Count == 1 &&
y.Parameters[0].Value as string == sourcePath
),
Arg<Action<object>>.Is.Equal((Action<object>)restExecution.Get)
)
);