0

I ma writing unit testcases using NUnit and Fake it Easy for MVC4 Controllers. In one of the controller, I should be getting exception.

     ExceptionDetail exception = new ExceptionDetail() { Description = "Test", ErrorCode = ExceptionErrorCode.UserIsDisabled };
     Exception ex = new Exception(new Uri("http://localhost//Plat/ManagementSvc/Groups?UserID=" + iD "), exception );

   A.CallTo(() => Fake.GetDataAsync<IEnumerable<Group>>(fakeHttpSession, url)).Throws(ex);

My question is instead of passing localhost(new Uri("http://localhost//Plat/ManagementSvc/Groups"), is there a way to fake the url for URI

AMDI
  • 895
  • 2
  • 17
  • 40
  • You're not executing any behaviour on the Uri, and a Uri doesn't rely on any outside service or anything. Why exactly do you want to fake it? You could replace the actual Uri text with anything else if you don't like localhost there (as an aside you should probably have only one `/` after "localhost", and you have an extra `"` after the `iD` in your sample code). What is your goal exactly? What isn't working for you? – Blair Conrad Sep 19 '16 at 09:44

1 Answers1

0

If the problem is that the exception isn't being thrown when your production code calls GetDataAsync, it could just be that your code is passing in a different Uri. If this is so, you can change which Uri triggers that call by using a different form of argument constraint.

Instead of matching the Uri exactly, you could choose to match any Uri:

A.CallTo(() => Fake.GetDataAsync<IEnumerable<Group>>(fakeHttpSession, A<Uri>.Ignored))
    .Throws(ex);

Or you could even be more specific:

A.CallTo(() => Fake.GetDataAsync<IEnumerable<Group>>(fakeHttpSession, A<Uri>.That.Matches(uri => uri.AbsolutePath == "/Plat/ManagementSvc/Groups"))
    .Throws(ex);

if that's what's needed. There are many options. Check out the documentation.

Note that there's no faking of a Uri here. You're just telling FakeItEasy what tests to apply to a received Uri in order to decide whether the faked GetDataAsync method should throw

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111