inside unit test I'm faking http context using
var fakeHttpContext = new Mock<HttpContextBase>();
var controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(t => t.HttpContext).Returns(fakeHttpContext.Object);
this.controller.ControllerContext = controllerContext.Object;
now inside my code I'm using following
UrlHelper helper = new UrlHelper(this.ControllerContext.RequestContext);
string url = helper.Action("Details", "Pers", new { id = person.Id });
what should I mock inside unit test in order to use UrlHelper code?
Currently UrlHelper helper is null.
p.s. Because of clarity I did not show whole initialization of controller code inside test, it's working, but I'm struggling now with this request context and UrlHelper.
Controller already has a UrlHelper Url property that you can pass an instance or mock. no need to new one up in the code.
Take a look at this example test with a controller that uses the UrlHelper.
[TestClass]
public class UrlHelperTest {
[TestMethod]
public void MockUrlHelper() {
//Arrange
var requestUrl = new Uri("http://myrequesturl");
var request = Mock.Of<HttpRequestBase>();
var requestMock = Mock.Get(request);
requestMock.Setup(m => m.Url).Returns(requestUrl);
var httpcontext = Mock.Of<HttpContextBase>();
var httpcontextSetup = Mock.Get(httpcontext);
httpcontextSetup.Setup(m => m.Request).Returns(request);
var actionName = "MyTargetActionName";
var expectedUrl = "http://myfakeactionurl.com";
var mockUrlHelper = new Mock<UrlHelper>();
mockUrlHelper
.Setup(m => m.Action(actionName, "Register", It.IsAny<object>(), It.IsAny<string>()))
.Returns(expectedUrl)
.Verifiable();
var sut = new MyController();
sut.Url = mockUrlHelper.Object;
sut.ControllerContext = new ControllerContext {
Controller = sut,
HttpContext = httpcontext,
};
//Act
var result = sut.MyAction();
//Assert
mockUrlHelper.Verify();
}
public class MyController : Controller {
[HttpPost]
public ActionResult MyAction() {
var link = GenerateActionLink("MyTargetActionName", string.Empty, string.Empty);
return View((object)link);
}
private string GenerateActionLink(string actionName, string token, string username) {
string validationLink = null;
if (Request.Url != null) {
var encodedToken = EncodedUrlParameter(token);
var url = Url.Action(actionName, "Register", new { Token = encodedToken, Username = username }, Request.Url.Scheme);
validationLink = url;
}
return validationLink;
}
private string EncodedUrlParameter(string token) {
return "Fake encoding";
}
}
}
Related
New to Unit Testing web api.
I am writing a Unit test to Test a controller and I have to mock Iconfiguration. The appsettings,json has a section called "AppSettings", I'm trying to mock it.
Also, the mock.setup returns null value in the controller causing it to fail.
Here is my controller:
private readonly ILogger _logger;
private readonly IConfiguration _configuration;
private readonly ICarPairingTable PairingTable;
private readonly ICarDealerSettingsTable DealerSettingsTable;
static AppSettings appSettings = null;
public CarController(IConfiguration configuration, ICarPairingTable carPairingTable, ICarDealerSettingsTable settingsTable)
{
_configuration = configuration;
appSettings = configuration.Get<AppSettingsModel>().AppSettings;
PairingTable = carPairingTable;
DealerSettingsTable = settingsTable;
}
[HttpGet]
public ActionResult Get(string id){
string DealerId ="";
DealerId = PairingTable.GetDealerId(id).Result;
if (string.IsNullOrEmpty(DealerId))
{
result = new ReturnResult
{
status = "Fail",
data = "ID is invalid"
};
return NotFound(result);
}
SettingsInfo info = DealerSettingsTable.GetSettingsInfo(DealerId).Result;
if (info == null)
{
result = new ReturnResult
{
status = "Fail",
data = "Not Found"
};
return NotFound(result);
}
result = new ReturnResult
{
status = "Success",
data = info
};
return Ok(result);
}
Here is my Unit Test:
[Fact]
public void Test1()
{
var mockConfig = new Mock<IConfiguration>();
var configurationSection = new Mock<IConfigurationSection>();
configurationSection.Setup(a => a.Value).Returns("testvalue");
mockConfig.Setup(a => a.GetSection("AppSettings")).Returns(configurationSection.Object);
var mock1 = new Mock<ICarPairingTable>();
mock1.Setup(p => p.GetDealerId("456")).ReturnsAsync("123");
var mock2 = new Mock<ICarDealerSettingsTable>();
SettingsInfo mockSettings = new SettingsInfo()
{
DealerId = "123",
Name="Dealer1"
};
mock2.Setup(p => p.GetSettingsInfo("123")).ReturnsAsync(()=>mockSettings);
CarController controller = new CarController(mockConfig.Object, mock1.Object, mock2.Object);
var result = controller.Get("456");
//Dont know what to assert
}
Wrote unit test, but not sure if my approach is correct, Help will be appreciated.
This is more of a design issue wrapped in an XY problem.
Really should not be injecting IConfiguration. Based on how the configuration is using by the controller what you should have done was register the settings with the service collection in startup
Startup.ConfigureServices
//...
AppSettings appSettings = Configuration.Get<AppSettingsModel>().AppSettings;
services.AddSingleton(appSettings);
//...
and explicitly inject the settings into the controller
//...
private readonly AppSettings appSettings = null;
public CarController(AppSettings appSettings , ICarPairingTable carPairingTable, ICarDealerSettingsTable settingsTable) {
this.appSettings = appSettings;
PairingTable = carPairingTable;
DealerSettingsTable = settingsTable;
}
//...
So now when unit testing the controller in isolation, you can initialize an instance of the desired class and provide when exercising the unit test.
Reference Explicit Dependencies Principle
You also appear to be mixing async-await and blocking calls like .Result.
I sugest you make the action async all the way
[HttpGet]
public async Task<ActionResult> Get(string id){
string DealerId = await PairingTable.GetDealerId(id);
if (string.IsNullOrEmpty(DealerId)) {
var result = new ReturnResult {
status = "Fail",
data = "ID is invalid"
};
return NotFound(result);
}
SettingsInfo info = await DealerSettingsTable.GetSettingsInfo(DealerId);
if (info == null) {
var result = new ReturnResult {
status = "Fail",
data = "Not Found"
};
return NotFound(result);
}
var result = new ReturnResult {
status = "Success",
data = info
};
return Ok(result);
}
Reference Async/Await - Best Practices in Asynchronous Programming
That way the unit test can finally be arranged correctly to verify the expected behavior
[Fact]
public async Task Should_Return_Ok_ReturnRsult() {
//Arrange
var id = "456";
var dealerId = "123";
SettingsInfo expected = new SettingsInfo() {
DealerId = dealerId,
Name="Dealer1"
};
var pairingMock = new Mock<ICarPairingTable>();
pairingMock.Setup(p => p.GetDealerId(id)).ReturnsAsync(dealerId);
var dealerSettingsMock = new Mock<ICarDealerSettingsTable>();
dealerSettingsMock.Setup(p => p.GetSettingsInfo(dealerId)).ReturnsAsync(() => expected);
CarController controller = new CarController(new AppSettings(), pairingMock.Object, dealerSettingsMock.Object);
//Act
var actionResult = await controller.Get(id);
var actual = actionResult as OkObjectResult;
//Assert (using FluentAssertions)
actual.Should().NotBeNull();
actual.Value.Should().BeOfType<ReturnResult>();
var actualResult = actual.Value as ReturnResult;
actualResult.data.Should().BeEquivalentTo(expected);
}
I'm trying to write test cases for my web api methods.
[Fact]
public async Task Get_Message_By_Id()
{
var messages = new Messages()
{
MessageId = 1,
Recipient = "1;2;3",
Subject = "Test Subject",
MessageText = "test subject"
};
var mesBL = new Mock<IMessagesBusinessLogic>();
mesBL
.Setup(repo => repo.GetRecivedMessageById(It.IsAny<IWebToken>() ,messages.MessageId))
.Returns(Task.FromResult(messages));
var messageController = new MessagesController(mesBL.Object);
var contentResult = await messageController.GetRecivedMessageById(messages.MessageId) as OkObjectResult;
Assert.NotNull(contentResult);
}
I get an error of null reference while GetRecivedMessageById method call.
Controller method;
[HttpGet]
[Route("{id:int}")]
public async Task<IActionResult> GetRecivedMessageById(int id)
{
return Ok(await _messagesBusinessLogic.GetRecivedMessageById(User.GetWebToken(), id));
}
Here, issue is because, user identity passing NULL.
How can we pass it from Test?
User is null because it was not setup in the test before exercising the method under test.
//Create test user
var displayName = "User name";
var role = "SomeRole";
var token = new Mock<IWebToken>();
token.Setup(m => m.DisplayName).Returns(displayName);
token.Setup(m => m.Role).Returns(role);
var identity = new UserIdentity(token.Object);
var principal = new GenericPrincipal(identity, roles: new string[] { });
var user = new ClaimsPrincipal(principal);
// Set the User on the controller via the ControllerContext
var messageController = new MessagesController(mesBL.Object) {
ControllerContext = new ControllerContext()
{
HttpContext = new DefaultHttpContext() { User = user }
}
};
Using the unit test below .. Iam trying to test my webapi.
[Test]
public void CheckControllerForCreate()
{
var config = new HttpConfiguration();
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/product");
var route = config.Routes.MapHttpRoute("Foo", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
var controller = new ProductController
{
ControllerContext = new HttpControllerContext(config, routeData, request),
Request = request,
Url = new UrlHelper(request)
};
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
var result = controller.Create(new Product {Id = 4, Name = "Tomato Soup", Category = "Groceries", Price = 1});
}
[HttpPost]
public HttpResponseMessage Create(Product product)
{
var url = Url;
if (product == null)
throw new HttpResponseException(new HttpResponseMessage{StatusCode = HttpStatusCode.BadRequest,ReasonPhrase = "Product is not specified"});
products.Add(product);
var response = Request.CreateResponse(HttpStatusCode.Created, product);
string uri = Url.Link("Foo", product.Id);
response.Headers.Location = new Uri(uri);
return response;
}
The Create Action throws an exception because uri is null. Now, the Url helper is correctly picking up the RouteName , otherwise there would be a RouteName not found exception. I am assuming that somethign is wrong with my configuration.
I referred to http://www.peterprovost.org/blog/2012/06/16/unit-testing-asp-dot-net-web-api and several other posts for unit testing the controllers.
The WebAPI method is here on codeplex
http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/1acb241299a8#src/System.Web.Http/Routing/UrlHelper.cs
Edit
I have narrowed it down to vpd being null in ( UrlHelper)
IHttpVirtualPathData vpd = configuration.Routes.GetVirtualPath(
request: request,
name: routeName,
values: routeValues);
Can't seem to figure out why ?
You need to set the routeData into the request, the same way you did with the configuration:
controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
Also you are incorrectly using the Url.Link helper. You haven't specified a controller nor you have indicated the id.
The code in your controller should look like this:
string uri = Url.Link("Foo", new { id = product.Id, controller = "product" });
UPDATE:
Here's a full example.
Controller:
public class ProductController : ApiController
{
public HttpResponseMessage Create(int id)
{
var uri = Url.Link("Foo", new { id = id, controller = "product" });
return Request.CreateResponse(HttpStatusCode.OK, uri);
}
}
Test:
// arrange
var config = new HttpConfiguration();
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/product");
var route = config.Routes.MapHttpRoute("Foo", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary(new { controller = "product" }));
var controller = new ProductController
{
ControllerContext = new HttpControllerContext(config, routeData, request),
Request = request,
Url = new UrlHelper(request)
};
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
controller.Request.Properties[HttpPropertyKeys.HttpRouteDataKey] = routeData;
// act
var result = controller.Create(4);
// assert
...
The helper:
public static MvcHtmlString RouteLink(this HtmlHelper helper, String linkText, String routeName, Object routeValues, String status)
{
if (status.ToLower() == "post-game" || status.ToLower() == "mid-game")
{
return helper.RouteLink(linkText, routeName, routeValues);
}
return MvcHtmlString.Create(" ");
}
The unit test:
[TestMethod]
public void RouteLinkTest()
{
var httpContext = new Mock<HttpContextBase>();
var routeData = new Mock<RouteData>();
var viewContext = new ViewContext { HttpContext = httpContext.Object, RouteData = routeData.Object };
var helper = new HtmlHelper(viewContext, new Mock<IViewDataContainer>().Object);
var target01 = helper.RouteLink("Linking Text", "route-name", new { id = "id" }, "status");
Assert.IsNotNull(target01);
}
The error:
Test method Web.Tests.Extensions.HtmlHelpersTest.RouteLinkTest threw exception:
System.ArgumentException: A route named 'route-name' could not be found in the route collection.
Parameter name: name
The question:
How do I mock the route to have the proper route name?
Instead of mockin the routevalues tell MvcApplication to register them
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
I use it when I have to test some redirectToAction in my controller with something like that
var controller = GetController();
var httpContext = Utilities.MockControllerContext(true, false).Object;
controller.ControllerContext = new ControllerContext(httpContext, new RouteData(), controller);
var routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
controller.Url = new UrlHelper(new RequestContext(httpContext, new RouteData()), routes);
var result = controller.DoMyStuff();
Assert.IsInstanceOfType(typeof(RedirectResult), result);
var actual = (RedirectResult)result;
Assert.AreEqual("/myurl", actual.Url.ToString());
I never tested an helper like yours but I think that should works
I am using ASP.NET MVC 3 and NUnit.
I created a helper method to to return an action method as such (overloaded method):
public static object CategoryIndex(this UrlHelper urlHelper)
{
return new { controller = "Category", action = "Index" };
}
public static string CategoryIndex(this UrlHelper helper, int categoryId)
{
return helper.RouteUrl(new { controller = "Category", action = "Index", id = categoryId });
}
The test that is failing is the second test called CategoryIndex_should_navigate_to_category_index_action_method_with_child_category_id().
private HttpContextBase httpContextBaseStub;
private RequestContext requestContext;
private UrlHelper urlHelper;
[SetUp]
public void SetUp()
{
httpContextBaseStub = MockRepository.GenerateStub<HttpContextBase>();
requestContext = new RequestContext(httpContextBaseStub, new RouteData());
urlHelper = new UrlHelper(requestContext);
}
[Test]
public void CategoryIndex_should_navigate_to_category_index_action_method()
{
// Act
object actual = UrlHelperNavigationExtensions.CategoryIndex(urlHelper);
// Assert
RouteValueDictionary routes = new RouteValueDictionary(actual);
Assert.AreEqual("Category", routes["controller"]);
Assert.AreEqual("Index", routes["action"]);
}
[Test]
public void CategoryIndex_should_navigate_to_category_index_action_method_with_child_category_id()
{
// Arrange
int childCategoryId = 1;
// Act
string actual = UrlHelperNavigationExtensions.CategoryIndex(urlHelper, childCategoryId);
// Assert
Assert.AreEqual("/Category/Index/1", actual);
}
It's complaining that actual is null. Why would this be and how would I rectify it?
it seems to me that you don't initialize the routecollection. I guess something like that would do the trick
[SetUp]
public void SetUp()
{
RouteCollection routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
httpContextBaseStub = MockRepository.GenerateStub<HttpContextBase>();
requestContext = new RequestContext(httpContextBaseStub, new RouteData());
//urlHelper = new UrlHelper(requestContext);
urlHelper = new UrlHelper(requestContext, routes);
}
It seems also, that you don't setup the HttpContextBase. Be sure to have it properly mocked or you will get some null reference exception.
[SetUp]
public void SetUp()
{
RouteCollection routes = new RouteCollection();
MvcApplication.RegisterRoutes(routes);
//httpContextBaseStub = (new Moq.Mock<HttpContextBase>()).Object;
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
response.Setup(r => r.ApplyAppPathModifier(It.IsAny<string>())).Returns((String url) => url);
var mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Setup(c => c.Request).Returns(request.Object);
mockHttpContext.Setup(c => c.Response).Returns(response.Object);
requestContext = new RequestContext(mockHttpContext.Object, new RouteData());
urlHelper = new UrlHelper(requestContext, routes);
}
My guess is because the route table is empty, so it doesn't know how to generate the url.
Add the normal /{controller}/{action} route and it should work.
(i'm on a phone so forgive if this is wrong)
You need to stub an IRouteHandler implementation, which I've called stub_handler here.
RouteTable.Routes.Add(new Route
(
"{controller}/{action}/{id}"
, stub_handler
);