REST Web Api unit test endpoint validity with NUnit and Moq 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&lt;IRestClient&gt;();
        stub.Setup(x =&gt; x.MakeRequest()).Throws&lt;UriFormatException&gt;();

        string results = new RestClient("example", "method=payment&amp;params[Key1]=Value1&amp;params[Key2]=Value2&amp;params[Key3]=Value3").MakeRequest();
    }


The RestClient class under test is

public class RestClient : IRestClient
{
    /// &lt;summary&gt;
    /// Web Api Url End point.
    /// &lt;/summary&gt;
    public string EndPoint { get; set; }

    /// &lt;summary&gt;
    /// The Post data.
    /// &lt;/summary&gt;
    public string PostData { get; set; }

    /// &lt;summary&gt;
    /// Do Request.
    /// &lt;/summary&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    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?
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:

public interface IFoo
{
     string SomeMethod(IBar bar);
}


public interfact IBar
{
    int CalculateSomething();
}


public class Foo : IFoo
{
     public string SomeMethod(IBar bar)
     {
         if (bar.CalculateSomething() &gt; 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&lt;IBar&gt;();
    bar.SetUp(b =&gt; 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.