1

I am using NUnit with FakeIteasy

My test Method:

  [Test]
    public async Task SiteDetailsView_NMSException()
    {

        url = "/Svc/v1/Sites/GetNextID?UID=" + orgUID;
        A.CallTo(() => fake.GetDataAsync<int>(fakeHttpSession, url)).Throws(new Exception(new Uri(url),
                      new ExceptionDetail()
                             {
                               ErrorCode=ExceptionErrorCode.ParameterOutOfRange
                               Description="param OrganizationUID"
                             }));
        // Act
        var actionResult = await myController.DetailsView(UID, oName, oUID, isReadOnly);
        var viewResult = actionResult as ViewResult;
           // Assert
        Assert.IsNotNull(actionResult);
    } 

My controller Code:



  public async Task<ActionResult> DetailsView(int sUID, string oName, int oUID, bool isReadOnly)
     {


             try
             {
                 SModel siModel = new SModel();


                 siModel .dite.Id = await _client.GetDataAsync<int>(Session, "/ManagementSvc/v1/ites/GetID?oUID=" + orgUID);

             }
             catch (Exception ex)
             {
                 return View("Error", _plExceptionHandler.HandleException(ex));
             }
         }
     }

I am getting error as

System.Uri.Format Exception : the format of URI cannot be determined.' at  
   A.CallTo(() => fake.GetDataAsync<int>(fakeHttpSession, url)).Throws(new SException(new Uri(url), new ExceptionDetail() { ErrorCode=ExceptionErrorCode.ParameterOutOfRange,Description="param OUID"}));

IS there any way to ignore the first(Uri) parameter and throw exception. I cannot pass null as well for the first parameter

here is my Exception class:

public class NException : Exception
{
    private readonly NExceptionDetail m_ExceptionDetails;

    private readonly Uri m_requestUri;

    public NException(Uri requestUri, nExceptionDetail nExceptionDetails)

    {
        if (requestUri == null)
           throw new ArgumentNullException("requestUri");

        m_requestUri = requestUri;

    }
}

How to throw NException using FakeItEasy.

I want to do assertions for the error messages.

How can i do assertion.

AMDI
  • 895
  • 2
  • 17
  • 40

1 Answers1

1

You get the UriFormatException because your original url variable is not a valid URI. That's why

.Throws(new NMSException(new Uri(url),
                  new NMSExceptionDetail()
                         {
                           ErrorCode=ExceptionErrorCode.ParameterOutOfRange
                           Description="param OrganizationUID"
                         }));

throws. You can see the same thing even without FakeItEasy:

var url = "/NMS/Platform/UserManagementSvc/v1/Sites/GetNextSiteID?organizationUID=" + 0;
new Uri(url);

To answer some of the other questions you bundled in here, you could ignore the first url like this:

A.CallTo(() => nmsFake.GetNMSPlatformDataAsync<int>(fakeHttpSession, A<string>.Ignored))

as seen in Ignoring Arguments in the FakeItEasy docs.

(Incidentally, you should get rid of one of your A.CallTos. I think you just put the second one in while you were trying some stuff out, so maybe you will take it out of your code in the end. While I'm on the subject, I encourage you to clean up your sample code, making it as small and targeted as possible while still reproducing the problem. This has two benefits:

  1. when things get simpler, you may see the answer yourself, and
  2. it makes it easier for us to understand your code and answer your questions)
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • Thanks for help for ignoring parameter.How can i over come the other problem regarding throwing custom exception class (NMSException) – AMDI Aug 24 '16 at 13:24
  • Since you're getting an error parsing the Uri, you can either stop parsing the `url` string (which would mean you'd need a `NMSExceptionDetail` constructor that took something other than a `Uri`), or construct a valid URI string in the `url` variable. – Blair Conrad Aug 24 '16 at 13:30
  • Thanks for the suggestions,let me know if there is any way to ignore that url parameter for URI. Becoz all my exception scenarios need to be covered from this. – AMDI Aug 24 '16 at 13:43
  • I'm not really sure what you're asking. In order to construct a `NMSException`, you need to call one of its constructors. If they all parse a URI string, then they will need to be passed a valid URI string. If you can substitute a different exception class, I suppose you could go that route. Otherwise, since `NMSException` appears to be your own class, you could provide an alternative constructor. But then your `m_requestUri` field may be invalid. Why do you not want to just provide a values URI string in your test? – Blair Conrad Aug 24 '16 at 15:23
  • Pardon me. I mispoke. The NMSException class doesn't parse the string, it just takes a `Uri`. The parsing happens one level up. But everything else applies. – Blair Conrad Aug 25 '16 at 12:30