I am unit testing that the URL that I give to my IRestClient
is valid. This client talks to a third party Web API.
The test is as below:
[Test]
[ExpectedException("System.UriFormatException")]
public void MakeRequest_EndPointIsInValid_ThrowsUriFormatException()
{
var stub = new Mock<IRestClient>();
stub.Setup(x => x.MakeRequest()).Throws<UriFormatException>();
string results = new RestClient("example", "method=payment¶ms[Key1]=Value1¶ms[Key2]=Value2¶ms[Key3]=Value3").MakeRequest();
}
The RestClient class under test is
public class RestClient : IRestClient
{
/// <summary>
/// Web Api Url End point.
/// </summary>
public string EndPoint { get; set; }
/// <summary>
/// The Post data.
/// </summary>
public string PostData { get; set; }
/// <summary>
/// Do Request.
/// </summary>
/// <returns></returns>
public string MakeRequest()
{
using (WebClient client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
if (string.IsNullOrWhiteSpace(PostData)) throw new ArgumentNullException("PostData");
if (string.IsNullOrWhiteSpace(EndPoint)) throw new ArgumentNullException("EndPoint");
// I want to test this Uri parsing.
Uri uriResult;
if (!Uri.TryCreate(EndPoint, UriKind.Absolute, out uriResult) || uriResult.Scheme != Uri.UriSchemeHttps) throw new UriFormatException("EndPoint");
return client.UploadString(uriResult, PostData);
}
}
}
How is this test and class under test?
Is my fake object a stub or a mock? I believe it's a stub as I am just forcing the return of a UriFormatException.
Any improvements?