I want to unit test my web API controller. I have a problem with one of my action method (POST) which is need value from Request object, to get the controller name.
I'm using NSubtitute, FluentAssertions to support my unit test
This is my controller code looks like:
public class ReceiptsController : BaseController
{
public ReceiptsController(IRepository<ReceiptIndex> repository) : base(repository) { }
..... Other action code
[HttpPost]
public IHttpActionResult PostReceipt(string accountId, [FromBody] ReceiptContent data, string userId = "", string deviceId = "", string deviceName = "")
{
if (data.date <= 0)
{
return BadRequest("ErrCode: Save Receipt, no date provided");
}
var commonField = new CommonField()
{
AccountId = accountId,
DeviceId = deviceId,
DeviceName = deviceName,
UserId = userId
};
return PostItem(repository, commonField, data);
}
}
And the base class for my controller :
public abstract class BaseController : ApiController
{
protected IRepository<IDatabaseTable> repository;
protected BaseController(IRepository<IDatabaseTable> repository)
{
this.repository = repository;
}
protected virtual IHttpActionResult PostItem(IRepository<IDatabaseTable> repo, CommonField field, IContent data)
{
// How can I mock Request object on this code part ???
string controllerName = Request.GetRouteData().Values["controller"].ToString();
var result = repository.CreateItem(field, data);
if (result.Error)
{
return InternalServerError();
}
string createdResource = string.Format("{0}api/accounts/{1}/{2}/{3}", GlobalConfiguration.Configuration.VirtualPathRoot, field.AccountId,controllerName, result.Data);
var createdData = repository.GetItem(field.AccountId, result.Data);
if (createdData.Error)
{
return InternalServerError();
}
return Created(createdResource, createdData.Data);
}
}
And this is my unit test for success create scenario:
[Test]
public void PostClient_CreateClient_ReturnNewClient()
{
// Arrange
var contentData = TestData.Client.ClientContentData("TestBillingName_1");
var newClientId = 456;
var expectedData = TestData.Client.ClientData(newClientId);
clientsRepository.CreateItem(Arg.Any<CommonField>(), contentData)
.Returns(new Result<long>(newClientId)
{
Message = ""
});
clientsRepository.GetItem(accountId, newClientId)
.Returns(new Result<ContactIndex>(expectedData));
// Act
var result = _baseController.PostClient(accountId, contentData, userId);
// Asserts
result.Should().BeOfType<CreatedNegotiatedContentResult<ContactIndex>>()
.Which.Content.ShouldBeEquivalentTo(expectedData);
}
I don't know if there is any way to extract Request object from the controller, or maybe is there any way to mock it on the unit test?
Right now this code Request.GetRouteData() return null on the unit test.
you can make an interface for getting Request Data(pass Request object to it). Implement that interface and use as dependency in your Controller. Then you can easily mock this interface implementation in your unit tests.
I've finally find a way to solve this. So basically I have to create some configuration related stuff to make my unit test works.
I create a helpers class for this
public static class Helpers
{
public static void SetupControllerForTests(ApiController controller)
{
var config = new HttpConfiguration();
var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/api/products");
var route = config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{id}");
var routeData = new HttpRouteData(route, new HttpRouteValueDictionary { { "controller", "products" } });
controller.ControllerContext = new HttpControllerContext(config, routeData, request);
controller.Request = request;
controller.Request.Properties[HttpPropertyKeys.HttpConfigurationKey] = config;
}
}
Then passing my test controller on my test setup
[SetUp]
public void SetUp()
{
clientsRepository = Substitute.For<IRepository<ContactIndex>>();
_baseController = new ClientsController(clientsRepository);
Helpers.SetupControllerForTests(_baseController);
}
I don't know if this is a best way to do it, but I prefer this way instead of create a new interface and inject it to my controller.
Related
I am needing to set up a Unit Test for our project. In our Controllers we are using a Unit of Work which holds all the repositories and calling the repository in our IHttpActionResult to get the data.
So far I have set up a Mock of the Interfaces and this is calling the correct IHttpActionResult in the Controller but there is no data coming back.
[TestMethod]
public void TestMethod1()
{
var unitOfWork = new Mock<IUnitOfWork>();
var repo = new Mock<IAuditReleaseRepository>();
unitOfWork.Setup(e => e.AuditReleaseRepository).Returns(repo.Object);
var auditReleaseController = new AuditReleaseController(unitOfWork.Object);
var result = auditReleaseController.Get() as ViewResult;
var model = result.ViewData.Model as IQueryable<AuditReleas>;
Assert.AreEqual(12, model.Count());
}
public class AuditReleaseController : BaseController
{
private IAuditReleaseRepository _auditReleaseRepository;
private IUnitOfWork _unitOfWork;
public AuditReleaseController(IUnitOfWork unitOfWork)
{
this._unitOfWork = unitOfWork;
this._auditReleaseRepository = unitOfWork.AuditReleaseRepository;
}
[HttpGet, Route("audit-releases")]
public IHttpActionResult Get()
{
var query = this._auditReleaseRepository.GetAll();
return Ok(query);
}
}
public class AuditReleaseRepository : RepositoryBase<AuditReleas>, IAuditReleaseRepository
{
private readonly RetailAssignmentEntities _entities;
public AuditReleaseRepository(RetailAssignmentEntities entities) : base(entities)
{
this._entities = entities;
}
IEnumerable<AuditReleaseDto> IDtoRepository<AuditReleaseDto>.GetAll()
{
return base.GetAll().Where(x=>x.IsReleaseEnabled).Select(AuditReleaseMapping.All).OrderByDescending(x => x.Id);
}
}
This is the Mapping that is taking place to get the data in the GetAll method:
public class AuditReleaseMapping
{
public static Expression<Func<AuditReleas, AuditReleaseDto>> All = (auditRelease) => new AuditReleaseDto()
{
EndDate = auditRelease.AuditReleaseEndDate,
Id = auditRelease.AuditReleaseId,
Name = auditRelease.AuditReleaseName,
StartDate = auditRelease.AuditReleaseStartDate,
AuditPeriodId = auditRelease.AuditPeriod.AuditPeriodId,
AuditYearId = auditRelease.AuditPeriod.AuditYear.AuditYearId,
AuditEndDate = auditRelease.AuditPeriod.AuditEndDate,
AuditStartDate = auditRelease.AuditPeriod.AuditStartDate
};
}
What would be ideal is to call the Controller, which would call the repository, which would then call the Mapping but so far it's not even calling the Repository to get the data. I need help getting that functionality set up.
It looks like you need to Setup the behavior for your Mock repo.GetAll(). You will need to create a list of AuditReleaseDto's for your Mocked repo to return. Call it TestAuditReleaseDtos. You can create this TestData in the beginning of your TestMethod or in the TesstClass initialization. Then use it in a line right after creating your Mock:
var repo = new Mock<IAuditReleaseRepository>();
repo.Setup(r => r.GetAll()).Returns(TestAuditReleaseDtos);
unitOfWork.Setup(e => e.AuditReleaseRepository).Returns(repo.Object);
I'm trying to test my project. I have never used tests before and I am starting to learn I would like a help, in the simplest case I want test this public ActionResult Index() but I don't know how to Inject those dependencies.
Controller:
Controller:
public class WorkPlacesController : Controller
{
private readonly IWorkPlaceService workPlaceService;
public WorkPlacesController(IWorkPlaceService workPlaceService)
{
this.workPlaceService = workPlaceService;
}
// GET: WorkPlaces
public ActionResult Index()
{
var workPlaces = workPlaceService.GetWorkPlaces(includedRelated:
true);
return View(workPlaces);
}
}
Here is my Service
Service
public class WorkPlaceService : IWorkPlaceService
{
private readonly IWorkPlaceRepository workPlacesRepository;
private readonly IUnitOfWork unitOfWork;
public WorkPlaceService(IWorkPlaceRepository workPlacesRepository, IUnitOfWork unitOfWork)
{
this.workPlacesRepository = workPlacesRepository;
this.unitOfWork = unitOfWork;
}
}
public interface IWorkPlaceService
{
IEnumerable<WorkPlace> GetWorkPlaces(string workPlaceDescription = null, bool includedRelated = true);
}
And my Repository
Repository
public class WorkPlaceRepository : RepositoryBase<WorkPlace>, IWorkPlaceRepository
{
public WorkPlaceRepository(IDbFactory dbFactory)
: base(dbFactory) { }
public WorkPlace GetWorkPlaceByDescription(string workPlaceDescription)
{
var workPlace = this.DbContext.WorkPlaces.Where(c => c.Description == workPlaceDescription).FirstOrDefault();
return workPlace;
}
}
public interface IWorkPlaceRepository : IRepository<WorkPlace>
{
WorkPlace GetWorkPlaceByDescription(string workPlaceDescription);
}
Factory
public class DbFactory : Disposable, IDbFactory
{
AgendaEntities dbContext;
public AgendaEntities Init()
{
return dbContext ?? (dbContext = new AgendaEntities());
}
protected override void DisposeCore()
{
if (dbContext != null)
dbContext.Dispose();
}
}
I tried to do something like this:
public void BasicIndexTest()
{
// Arrange
var mockRepository = new Mock<IWorkPlaceService>();
var controller = new WorkPlacesController(mockRepository.Object);
// Act
ActionResult actionResult = controller.Index() as ViewResult;
// Assert
Assert.IsInstanceOfType(actionResult, typeof(List<WorkPlace>));
}
How do I inject in this controller the data needed to go in the database and bring the results?
I Want test this public ActionResult Index() but I don't know how to Inject those dependencies.
Mock the behavior of required dependencies of the controller for the test and assert the desired behavior when the test is exercised.
For example, based on what you have done so far
public void BasicIndexTest() {
// Arrange
var mockService = new Mock<IWorkPlaceService>();
var workPlaces = new List<WorkPlace>() {
new WorkPlace()
};
mockService
.Setup(_ => _.GetWorkPlaces(It.IsAny<string>(), It.IsAny<bool>()))
.Returns(workPlaces);
var controller = new WorkPlacesController(mockService.Object);
// Act
var actionResult = controller.Index() as ViewResult;
// Assert
Assert.IsNotNull(actionResult);
var model = actionResult.Model;
Assert.IsNotNull(model)
Assert.IsInstanceOfType(model, typeof(List<WorkPlace>));
Assert.AreEqual(workPlaces, model);
}
Only the IWorkPlaceService was needed for the testing of Index action, but fake data was needed for the invocation of the GetWorkPlaces method. So the mock was configured to return a list of objects when called and pass it to the view result.
Hi everyone I am new to testing so please go easy :). I'm having trouble testing my api controllers. I have created a separate class to test the controller but I having issues with 'Object reference not set to valid instance'
The api controller and method I want to test:
namespace Project.Controllers.Api
{
[Authorize]
[Route("/api/entries/{date}")]
public class EntryController : Controller
{
private ILogger<EntryController> _logger;
private ITrackerRepository _repository;
public EntryController(ITrackerRepository repository, ILogger<EntryController> logger)
{
_repository = repository;
_logger = logger;
}
[HttpGet("")]
public JsonResult Get(string date)
{
// DateTime date mm/dd/yyyy
DateTime dateTime = Convert.ToDateTime(date);
var result = Mapper.Map<IEnumerable<EntryViewModel>>(_repository.GetDiaryEntries(dateTime, User.Identity.Name));
if (result != null)
{
return Json(result);
}
else
{
return Json(null);
}
}
}
my attempt to unit test with xunit:
namespace test.Project.UnitTests
{
public class EntryControllerTest
{
public EntryControllerTest()
{
}
[Fact]
public void TestInvalidViewModels()
{
// Created test mock/empty repository
TestProjectRepository testRepo = new TestProjectRepository();
var controller = new EntryController(testRepo, null);
var result = controller.Get("01/12/1899");
Assert.Equal(result, null);
}
}
}
I get the issue that 'Object reference is not set to an instance of an object" in the Controller.cs file. Any help much appreciated, thank you.
From your code, this User.Identity.Name is your most likely reason for the error.
[Fact]
public void TestInvalidViewModels()
{
//Arrange
var username = "username#example.com";
var identity = new GenericIdentity(username, "");
var fakeUser = new GenericPrincipal(identity, roles: new string[] { });
TestProjectRepository testRepo = new TestProjectRepository();
var controller = new EntryController(testRepo, null);
controller.User = fakeUser;
//Act
var result = controller.Get("01/12/1899");
//Assert
Assert.Equal(result, null);
}
I want to test if a URL is part of the routes defined in the Global.asax. This is what I have:
var TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString();
var TheRoutes = System.Web.Routing.RouteTable.Routes;
foreach (var TheRoute in TheRoutes)
{
if (TheRequest == TheRoute.Url) //problem here
{
RequestIsInRoutes = true;
}
}
The problem is that I can’t extract the URL from the route. What do I need to change?
The problem is that I can't extract the URL from the route.
I disagree. The problem is that you expect to pull the URLs out of the route table and compare them externally. Furthermore, it is unclear what you hope to gain by doing so.
Routing compares the incoming request against business logic to determine if it matches. This is a route's purpose. Moving the matching logic outside of the route is not a valid test because you are not testing the business logic that is implemented by the route.
Not to mention, it is a bit presumptive to assume that a route can only match a URL and nothing else in the request such as form post values or cookies. While the built in routing functionality only matches URLs, there is nothing stopping you from making a constraint or custom route that matches other criteria.
So, in short you need to write unit tests for the business logic in your routes. Any logic that happens outside of your route configuration should be unit tested separately.
There is a great post by Brad Wilson (albeit a bit dated) that demonstrates how to unit test your routes. I have updated the code to work with MVC 5 - here is a working demo using the below code.
IncomingRouteTests.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcRouteTesting;
using System.Web.Mvc;
using System.Web.Routing;
[TestClass]
public class IncomingRouteTests
{
[TestMethod]
public void RouteWithControllerNoActionNoId()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/controller1");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.AreEqual("controller1", routeData.Values["controller"]);
Assert.AreEqual("Index", routeData.Values["action"]);
Assert.AreEqual(UrlParameter.Optional, routeData.Values["id"]);
}
[TestMethod]
public void RouteWithControllerWithActionNoId()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/controller1/action2");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.AreEqual("controller1", routeData.Values["controller"]);
Assert.AreEqual("action2", routeData.Values["action"]);
Assert.AreEqual(UrlParameter.Optional, routeData.Values["id"]);
}
[TestMethod]
public void RouteWithControllerWithActionWithId()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/controller1/action2/id3");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.AreEqual("controller1", routeData.Values["controller"]);
Assert.AreEqual("action2", routeData.Values["action"]);
Assert.AreEqual("id3", routeData.Values["id"]);
}
[TestMethod]
public void RouteWithTooManySegments()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/a/b/c/d");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNull(routeData);
}
[TestMethod]
public void RouteForEmbeddedResource()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/foo.axd/bar/baz/biff");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
Assert.IsInstanceOfType(routeData.RouteHandler, typeof(StopRoutingHandler));
}
}
OutgoingRouteTests.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcRouteTesting;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
[TestClass]
public class OutgoingRouteTests
{
[TestMethod]
public void ActionWithAmbientControllerSpecificAction()
{
UrlHelper helper = GetUrlHelper();
string url = helper.Action("action");
Assert.AreEqual("/defaultcontroller/action", url);
}
[TestMethod]
public void ActionWithSpecificControllerAndAction()
{
UrlHelper helper = GetUrlHelper();
string url = helper.Action("action", "controller");
Assert.AreEqual("/controller/action", url);
}
[TestMethod]
public void ActionWithSpecificControllerActionAndId()
{
UrlHelper helper = GetUrlHelper();
string url = helper.Action("action", "controller", new { id = 42 });
Assert.AreEqual("/controller/action/42", url);
}
[TestMethod]
public void RouteUrlWithAmbientValues()
{
UrlHelper helper = GetUrlHelper();
string url = helper.RouteUrl(new { });
Assert.AreEqual("/defaultcontroller/defaultaction", url);
}
[TestMethod]
public void RouteUrlWithAmbientValuesInSubApplication()
{
UrlHelper helper = GetUrlHelper(appPath: "/subapp");
string url = helper.RouteUrl(new { });
Assert.AreEqual("/subapp/defaultcontroller/defaultaction", url);
}
[TestMethod]
public void RouteUrlWithNewValuesOverridesAmbientValues()
{
UrlHelper helper = GetUrlHelper();
string url = helper.RouteUrl(new
{
controller = "controller",
action = "action"
});
Assert.AreEqual("/controller/action", url);
}
static UrlHelper GetUrlHelper(string appPath = "/", RouteCollection routes = null)
{
if (routes == null)
{
routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
}
HttpContextBase httpContext = new StubHttpContextForRouting(appPath);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "defaultcontroller");
routeData.Values.Add("action", "defaultaction");
RequestContext requestContext = new RequestContext(httpContext, routeData);
UrlHelper helper = new UrlHelper(requestContext, routes);
return helper;
}
}
Stubs.cs
using System;
using System.Collections.Specialized;
using System.Web;
public class StubHttpContextForRouting : HttpContextBase
{
StubHttpRequestForRouting _request;
StubHttpResponseForRouting _response;
public StubHttpContextForRouting(string appPath = "/", string requestUrl = "~/")
{
_request = new StubHttpRequestForRouting(appPath, requestUrl);
_response = new StubHttpResponseForRouting();
}
public override HttpRequestBase Request
{
get { return _request; }
}
public override HttpResponseBase Response
{
get { return _response; }
}
public override object GetService(Type serviceType)
{
return null;
}
}
public class StubHttpRequestForRouting : HttpRequestBase
{
string _appPath;
string _requestUrl;
public StubHttpRequestForRouting(string appPath, string requestUrl)
{
_appPath = appPath;
_requestUrl = requestUrl;
}
public override string ApplicationPath
{
get { return _appPath; }
}
public override string AppRelativeCurrentExecutionFilePath
{
get { return _requestUrl; }
}
public override string PathInfo
{
get { return ""; }
}
public override NameValueCollection ServerVariables
{
get { return new NameValueCollection(); }
}
}
public class StubHttpResponseForRouting : HttpResponseBase
{
public override string ApplyAppPathModifier(string virtualPath)
{
return virtualPath;
}
}
With that out of the way, back to your original question.
How to determine if the URL is in the route table?
The question is a bit presumptive. As others have pointed out, the route table does not contain URLs, it contains business logic. A more correct way to phrase the question would be:
How to determine if an incoming URL matches any route in the route table?
Then you are on your way.
To do so, you need to execute the GetRouteData business logic in the route collection. This will execute the GetRouteData method on each route until the first one of them returns a RouteData object instead of null. If none of them return a RouteData object (that is, all of the routes return null), it indicates that none of the routes match the request.
In other words, a null result from GetRouteData indicates that none of the routes matched the request. A RouteData object indicates that one of the routes matched and it provides the necessary route data (controller, action, etc) to make MVC match an action method.
So, to simply check whether a URL matches a route, you just need to determine whether the result of the operation is null.
[TestMethod]
public void EnsureHomeAboutMatches()
{
// Arrange
var context = new StubHttpContextForRouting(requestUrl: "~/home/about");
var routes = new RouteCollection();
RouteConfig.RegisterRoutes(routes);
// Act
RouteData routeData = routes.GetRouteData(context);
// Assert
Assert.IsNotNull(routeData);
}
Note also that generating routes is a separate task from matching incoming routes. You can generate outgoing URLs from routes, but it uses a completely different set of business logic than matching incoming routes. This outgoing URL logic can (and should) be unit tested separately from the incoming URL logic as demonstrated above.
I don't know if this is what you want is the requested route, if that is the case you can get it from the current request:
var route = HttpContext.Current.Request.RequestContext.RouteData.Route;
You could try checking the current context against route table
var contextBase = HttpContext.Current.Request.RequestContext.HttpContext;
var data = RouteTable.Routes.GetRouteData(contextBase);
if (data != null) {
//Route exists
}
Using the above as a basis of creating a service
public interface IRouteInspector {
bool RequestIsInRoutes();
}
public interface IHttpContextAccessor {
HttpContextBase HttpContext { get; }
}
public interface IRouteTable {
RouteCollection Routes { get; }
}
public class RouteInspector : IRouteInspector {
private IRouteTable routeTable;
private IHttpContextAccessor contextBase;
public RouteInspector(IRouteTable routeTable, IHttpContextAccessor contextBase) {
this.routeTable = routeTable;
this.contextBase = contextBase;
}
public bool RequestIsInRoutes() {
if (routeTable.Routes.GetRouteData(contextBase.HttpContext) != null) {
//Route exists
return true;
}
return false;
}
}
And here is test class showing how it is used.
[TestClass]
public class RouteTableUnitTests : ControllerUnitTests {
[TestMethod]
public void Should_Get_Request_From_Route_Table() {
//Arrange
var contextBase = new Mock<IHttpContextAccessor>();
contextBase.Setup(m => m.HttpContext)
.Returns(HttpContext.Current.Request.RequestContext.HttpContext);
var routeTable = new Mock<IRouteTable>();
routeTable.Setup(m => m.Routes).Returns(RouteTable.Routes);
var sut = new RouteInspector(routeTable.Object, contextBase.Object);
//Act
var actual = sut.RequestIsInRoutes();
//Assert
Assert.IsTrue(actual);
}
}
There is room for refactoring and improvements but it's a start.
This is what I ended up doing:
string TheRequest = HttpContext.Current.Request.Url.AbsolutePath.ToString();
foreach (Route r in System.Web.Routing.RouteTable.Routes)
{
if (("/" + r.Url) == TheRequest)
{
//the request is in the routes
}
}
It's hacky but it works in 3 lines.
I'm trying to Setup a Mock call to a the IModelFactory interface that I have.
here is the IModelFactory interface
public interface IModelFactory
{
MaterialAcceptedModel Create(MaterialAccepted model);
MaterialAccepted Parse(MaterialAcceptedModel model);
}
This is the ModelFactory class which implements the IModelFactor interface (I've only add here the method I'm trying to test, no need to add the implementation of the Create Method)
public class ModelFactory : IModelFactory
{
private UrlHelper _urlHelper;
private IRTWRepository _repo;
//private IKeysGeneratorService _keysGen;
private IGeocodeService _geocoder;
public ModelFactory(HttpRequestMessage request, IRTWRepository repo,IGeocodeService geocoder)
{
_urlHelper = new UrlHelper(request);
_repo = repo;
_geocoder = geocoder;
}
#region Parses
public MaterialAccepted Parse(MaterialAcceptedModel model)
{
try
{
if (!string.IsNullOrWhiteSpace(model.category))
{
var category = _repo.CategoryRepository.Get(model.category);
if (category == null) return null;
var entry = new MaterialAccepted()
{
business = model.business,
businessService = model.businessService,
residential = model.residential,
residentialService = model.residentialService,
note = model.note,
Category = category
};
return entry;
}
return null;
}
catch
{
return null;
}
}
#endregion
}
I'm using a BaseAPiController that contains the repo and configuration Interfaces
public class BaseApiController : ApiController
{
IRTWRepository _repository;
IModelFactory _modelFactory;
IConfiguration _configuration;
IGeocodeService _geocoder;
public BaseApiController(IRTWRepository repository,IConfiguration configuration)
{
_repository = repository;
_configuration = configuration;
}
protected IRTWRepository TheRepository
{
get
{
return _repository;
}
}
protected IConfiguration TheConfiguration
{
get
{
return _configuration;
}
}
protected IModelFactory TheModelFactory
{
get
{
_geocoder = new GeocodeService(_configuration.GetValue("geocodeGoogleApiKey"));
if (_modelFactory == null)
{
_modelFactory = new ModelFactory(this.Request, _repository,_geocoder);
}
return _modelFactory;
}
}
Here is the action method in the controller I'm trying to test
[HttpPost]
[Route("api/recyclecenters/{rcid}/materials/")]
public IHttpActionResult Post(int rcid, [FromBody]MaterialAcceptedModel model)
{
try
{
if (model != null)
{
var recycleCenter = TheRepository.RecycleCenterRepository.Get(rcid);
if (recycleCenter == null)
return NotFound();
if (!ModelState.IsValid)
return BadRequest(ModelState);
var entity = TheModelFactory.Parse(model);
if (entity == null) return BadRequest("Could not read material accepted in body");
if (TheRepository.MaterialAcceptedRepository.Get(recycleCenter.RecycleCenterId, entity.Category.name) != null)
return Conflict();
recycleCenter.Materials.Add(entity);
if (TheRepository.SaveAll())
{
string locationHeader = Url.Link("Materials", new { rcid = rcid, name = model.category.ToLower() });
return Created<MaterialAcceptedModel>(locationHeader, TheModelFactory.Create(entity));
}
return BadRequest("Could not save to the database");
}
return BadRequest();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
This is the line that returns null even though I mocked it up on my test method
var entity = TheModelFactory.Parse(model);
and this one is my TestClass
namespace API.Tests.Web
{
[TestClass]
public class MaterialsControllerTest
{
private Mock<IRTWRepository> repository;
private Mock<IModelFactory> factory;
private Mock<IConfiguration> configuration;
private Mock<IRTWAPIIdentityService> identityService;
private MaterialsController controller;
RecycleCenter recycleCenter;
private MaterialAccepted CreateMaterial()
{
return new MaterialAccepted()
{
business = true,
businessService = EnumRecycleCenterService.Dropoff,
residential = false,
residentialService = EnumRecycleCenterService.Pickup,
note = "this a note",
Category = new Category()
{
name = "Books"
}
};
}
[TestInitialize]
public void Initialize()
{
repository = new Mock<IRTWRepository>();
factory = new Mock<IModelFactory>();
configuration = new Mock<IConfiguration>();
identityService = new Mock<IRTWAPIIdentityService>();
controller = new MaterialsController(repository.Object,configuration.Object);
controller.Request = new HttpRequestMessage();
recycleCenter = new RecycleCenter(){RecycleCenterId = 1};
}
[TestMethod]
public void Post_ShouldReturnConflictIfTheRecycleCenterAlreadyTakesMaterial()
{
//arrange
repository.Setup(r => r.RecycleCenterRepository.Get(It.IsAny<int>())).Returns(() => recycleCenter);
factory.Setup(f => f.Parse(new MaterialAcceptedModel())).Returns(() => new MaterialAccepted());
configuration.Setup(c => c.GetValue(It.IsAny<string>())).Returns(() => "APIKEY");
repository.Setup(r => r.MaterialAcceptedRepository.Get(It.IsAny<int>(), It.IsAny<string>())).Returns(() => null);
//act
var actionResult = controller.Post(It.IsAny<int>(),new MaterialAcceptedModel());
//assert
Assert.IsInstanceOfType(actionResult, typeof(ConflictResult));
}
}
}
This is the line that's not working because it always return null instead of a new instance of MaterialAccepted
factory.Setup(f => f.Parse(new MaterialAcceptedModel())).Returns(() => new MaterialAccepted());
I tried f.Parse(It.IsAny()) but still doesn't work.
To clarify
the above line of code is returning null because is not mocking the f.Parse() call, instead is executing it and returning null because the if condition I have on that method
Anyone could explain why the Setup is not working?
Setting up your Mock using It.IsAny will work:
factory.Setup(f => f.Parse(It.IsAny<MaterialAcceptedModel>()))
.Returns(() => new MaterialAccepted());
However, as has been said by #Macilquham, I can't see where your are passing the Mock to your controller in the supplied code so that it is used by the production code.
If you don't call the method on your Mock object, which you don't, you're currently calling the method on the instance of the real object created by your base class, then it doesn't matter how you set up your mock it's not going to work. If you are able to change your base class, then doing something like this would allow you to work around your problem:
// Add defaulted parameter to base class to allow modelFactory creation
// to be overridden/injected (this will prevent the current default behaviour
// when fetching the factory, if a non-null is passed in)
public BaseApiController(IRTWRepository repository,IConfiguration configuration,
IModelFactory modelFactory = null)
{
_modelFactory = modelFactory;
}
Modify your sut constructor to allow you to supply a modelFactory (again, default it to null), then amend your test as appropriate:
controller = new MaterialsController(repository.Object,configuration.Object,
factory.Object);
You don't seem to be injecting in the IModelFactory into the controller. You need to make sure that your production code is using the Mock you are setting up in the test.
Mock cannot return null directly.
The trick is just to create a null object.
Assuming the object returned is of type class Material:
Material nullMaterial = null;
...
repository.Setup(r => r.MaterialAcceptedRepository
.Get(It.IsAny<int>(), It.IsAny<string>()))
.Returns(nullMaterial);
This should solve your problem