patterncsharpMinor
REST Web Api unit test endpoint validity with NUnit and Moq
Viewed 0 times
endpointrestwithnunitmoqvaliditytestwebandapi
Problem
I am unit testing that the URL that I give to my
The test is as below:
The RestClient class under test is
-
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?
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();
stub.Setup(x => x.MakeRequest()).Throws();
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
{
///
/// Web Api Url End point.
///
public string EndPoint { get; set; }
///
/// The Post data.
///
public string PostData { get; set; }
///
/// Do Request.
///
///
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?
Solution
I think you are confused about what a mock is actually supposed to be used for. A mock is to create a fake object to pass into a parameter, and is very useful when testing systems that use dependency injection exclusively.
Take this for example:
One of your tests is to ensure the
This is really simple, and I hope there is more to the
To make your test pass, you would simply have to pass in an invalid uri string. No Mock needed.
Take this for example:
public interface IFoo
{
string SomeMethod(IBar bar);
}
public interfact IBar
{
int CalculateSomething();
}
public class Foo : IFoo
{
public string SomeMethod(IBar bar)
{
if (bar.CalculateSomething() > 10)
{
// insert more code
return "Completed";
}
return string.Empty;
}
}One of your tests is to ensure the
// insert more code section works. To get into that code you need the IBar.CalculateSomething() to return a value > 10. You also don't want to create an instance of the Bar class, because then you are not testing one unit of code. This is where mocking comes into play. The way to make sure you hit the code is to pass a mock into the method, that is set up to pass the if statement[Test]
public void EnsureThatInserMoreCodeWorks()
{
var bar = Mock.Create();
bar.SetUp(b => b.CalculateSomething()).Returns(15);
var sut = new Foo();
var result = sut.SomeMethod(bar.Object);
Assert.That(result, Is.EqualTo("Completed");
}This is really simple, and I hope there is more to the
// insert more code section to test, but this gives an easy to follow example of using mocks.To make your test pass, you would simply have to pass in an invalid uri string. No Mock needed.
Code Snippets
public interface IFoo
{
string SomeMethod(IBar bar);
}
public interfact IBar
{
int CalculateSomething();
}
public class Foo : IFoo
{
public string SomeMethod(IBar bar)
{
if (bar.CalculateSomething() > 10)
{
// insert more code
return "Completed";
}
return string.Empty;
}
}[Test]
public void EnsureThatInserMoreCodeWorks()
{
var bar = Mock.Create<IBar>();
bar.SetUp(b => b.CalculateSomething()).Returns(15);
var sut = new Foo();
var result = sut.SomeMethod(bar.Object);
Assert.That(result, Is.EqualTo("Completed");
}Context
StackExchange Code Review Q#28072, answer score: 4
Revisions (0)
No revisions yet.