Reputation: 5090
I have a method and it is also the method that I am trying to write a test for:
public IPlayerResponse GetPlayerStats(IPlayerRequest playerRequest)
{
string playerId = playerRequest.PlayerId;
string requestUri = playerId + "/evaluate";
var request = new HttpRequestMessage(HttpMethod.Post, requestUri)
{
Content = new StringContent
(JsonConvert.SerializeObject(playerRequest),
System.Text.Encoding.UTF8,
"application/json")
};
var response = _client
.SendAsync(request, CancellationToken.None)
.Result;
var responseContent = response.
Content
.ReadAsStringAsync()
.Result;
var playerResponseModel =
JsonConvert.DeserializeObject<PlayerResponseModel>(responseContent);
var playerResponse = _mapper
.Map<IPlayerResponse>(playerResponseModel);
return playerResponse;
}
The request interface:
public interface IPlayerRequest
{
IEnumerable<IPlayerStatsItem> Items { get; set; }
string PlayerId { get; set; }
}
I plan to write a test for this method and send the sample data for IPlayerRequest
to get the response against a deployed end point. I have the TestSetup ready for hitting the endpoint.
What is the ideal way to mock the IPlayerRequest
and populate it with some data to get a response?
Upvotes: 0
Views: 739
Reputation: 300
I lack experience in using mocking framework as Moq or RhinoMocks, something on MS fakes, which works pretty good for stubbing(mocking) & shimming
If you have Visual Studio Premium or Ultimate you could always use MS fakes! (https://msdn.microsoft.com/en-us/library/hh549175.aspx)
Simply stub the interface you're trying to replace with the stub, which is explained in the post.
U can then use it as follows :
[TestMethod]
public void TestSomething()
{
// Arrange
// Stub for PlayerRequest
StubIPlayerRequest playerRequest = new StubIPlayerRequest
{
ItemsSetIEnumerableOfIPlayerStatsItem = items => { }, // Setters do nothing in this example.
ItemsGet = () => new List<StubIPlayerStatsItem>(), // Also stubbed the other interface here, not required.
PlayerIdGet = () => "Returns a string",
PlayerIdSetString = s => { }
};
// Act
// Assert whatever you need.
}
It's a pretty easy and straightforward framework for stubbing and shimming if you need in it your unit testing without downloading libraries.
Some interesting discussions on differences -- > Mock framework vs MS Fakes frameworks
HTH.
Upvotes: 0
Reputation: 22320
There is another syntax (Moq functional specifications) for setting up mock for cases like this:
var mockRequest = Mock.Of<IPlayerRequest>(
r =>
r.PlayerId == "MOCKID" &&
r.Items == new List<IPlayerStatsItem>()); //or whatever needed);
There is also Mocks.Of<T>
, so instead of using r.Items == new List
you can do r.Items == Mocks.Of<IPlayerStatsItem>()
. This will produce infinite list of mocks.
Upvotes: 1
Reputation: 1
In your calling method in the test setup just make a private class.
private class PlayerRequest : IPlayerRequest
{
//hard code your test values you are passing in
public string Name {get; private set;}
public PlayerRequest() {
Name = "MockName";
}
}
then call:
var response = obj.GetPlayerStatus(new PlayerRequest());
edited to add the moq version if that is what you were trying to use:
var mockRequest = new Mock<IPlayerRequest>();
mockRequest.SetupGet(pr => pr.Name).Returns("MOCKNAME");
var response = obj.GetPlayerStatus(mockRequest.Object);
Upvotes: 0