I am trying to learn TDD/BDD using NUnit and Moq.
The design that I have been following passes a DataService class to my controller to provide access to repositories.
I would like to Mock the DataService class to allow testing of the controllers.
There are lots of examples of mocking a repository passed to the controller but I can't work out how to mock a DataService class in this
scenerio.
Could someone please explain how to implement this?
Here's a sample of the relevant code:
[Test]
public void Can_View_A_Single_Page_Of_Lists()
{
var dataService = new Mock<DataService>();
var controller = new ListsController(dataService);
...
}
namespace Services
{
public class DataService
{
private readonly IKeyedRepository<int, FavList> FavListRepository;
private readonly IUnitOfWork unitOfWork;
public FavListService FavLists { get; private set; }
public DataService(IKeyedRepository<int, FavList> FavListRepository,
IUnitOfWork unitOfWork)
{
this.FavListRepository = FavListRepository;
this.unitOfWork = unitOfWork;
FavLists = new FavListService(FavListRepository);
}
public void Commit()
{
unitOfWork.Commit();
}
}
}
namespace MyListsWebsite.Controllers
{
public class ListsController : Controller
{
private readonly DataService dataService;
public ListsController(DataService dataService)
{
this.dataService = dataService;
}
public ActionResult Index()
{
var myLists = dataService.FavLists.All().ToList();
return View(myLists);
}
}
}
Create an interface like this:
public interface DataService
{
FavListService FavLists { get; }
void Commit();
}
Make your DataService implement this interface and your controller should depend on this interface. Problem solved :)
EDIT: This line of code:
dataService.FavLists.All().ToList();
is breaking the law of demeter and will be a pain to unit test your service. Create a method like AllFavList() on your service instead of all these chain of calls, it will be easier to mock.
EDIT2: How to mock you get property
dataService.SetupGet(d => d.FavLists).Returns(your_variable);
Related
I'm new to ASP.NET Core and I'm trying to solve this problem for a week now.
I have a solution with two projects.
And when I start the porject the browser just says:
InvalidOperationException: Unable to resolve service for type 'TSM_Programm.Data.TSMContext' while attempting to activate 'TSM_Programm.Controllers.ResourcesController'.
The first part of the solution is my API-Layer that passes data to a user (currently via postman).
The second project is my Data Access Layer.
This Layer contains several Controllers, all of them using the same constructor, which is the following:
public TSMContext _context;
public ResourcesController(TSMContext context)
{
_context = context;
}
The TSMContext Class is the following:
namespace TSM_Programm.Data
{
public class TSMContext : DbContext
{
public TSMContext(DbContextOptions<TSMContext> options)
: base(options)
{
}
public DbSet<Resource> Resources { get; set; }
public DbSet<Parameter> Parameters { get; set; }
public DbSet<ResourceToParameter> ResourceToParameters { get; set; }
public DbSet<Reservation> Reservations { get; set; }
}
So far so god, but when I am trying to start the program the controllerof the API-Layer does not seem to be able to handle the constructor.
This is my API-Conrtoller:
namespace TSM_API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class APIController : ControllerBase //Base Class without View Support
{
//Troublemaker
public ResourcesController _resources;
public ParametersController _parameters;
public ReservationsController _reservations;
public APIController(ResourcesController resources, ParametersController parameters, ReservationsController reservations)
{
_resources = resources;
_parameters = parameters;
_reservations = reservations;
}
//Function to check if controller works
//GET: api/API
[HttpGet]
public IEnumerable<string> Get()
{
// ResourcesController controller = new ResourcesController();
return new string[] { "value1", "value2" };
}
The API-Controller was not able to use its own constructors, that's why I changed the Startup.cs.
namespace TSM_API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc().AddApplicationPart(typeof(ResourcesController).Assembly).AddControllersAsServices();
services.AddMvc().AddApplicationPart(typeof(ParametersController).Assembly).AddControllersAsServices();
services.AddMvc().AddApplicationPart(typeof(ReservationsController).Assembly).AddControllersAsServices();
services.AddMvc().AddApplicationPart(typeof(TSMContext).Assembly).AddControllersAsServices();
}
I'm simply out of ideas on how to solve the problem, since I can't add the TSMContext class a service.
Any idea how to solve it?
Thank you.
I see you have not registered your dbcontext as a dependency injection. Your issue might be due to ResourceController trying to access _context as a DI but it is not registered. To use the context as a dependency injection, register it in the startup.cs as following.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<TSMContext>(options => options.UseSqlServer(Configuration.GetConnectionString("YOUR_CONNECTION_STRING")));
//If you have any services that should be used as DI, then they also must be registered as like this
services.AddScoped<Interface, Class>(); //Interface refer to the service interface while class is the actual service you will use.
}
I search about base controller in asp.net mvc 6 however there is no any source (as far as i check).So how can i add base controller in asp.net mvc and use services on constuctor method or create new methods in base controller or any idea in order to use anything like base controller?
Any help will be appreciated.
Thanks.
You can add base controller in the following way:
public class BaseController : Controller
{
public IService Service { get; }
public BaseController(IService service)
{
Service = service;
}
}
Then, you can create your own controller and inherit BaseController instead of Controller class.
public class NewController : BaseController
{
public NewController(IService service) : base(service)
{
}
public IActionResult NewAction()
{
var result = Service.ServiceMethod();
}
}
With Microsoft.Extensions.DependencyInjection name space gives us access to the following extension method HttpContext.RequestServices.GetService
Here’s the source code of our BaseController class
public abstract class BaseController<T> : Controller where T : BaseController<T>
{
private IService service;
protected IService _service => telemetryInitializer ?? (telemetryInitializer = HttpContext.RequestServices.GetService<West.TelemetryService.ITelemetryHelper>());
}
The OrderController class extends this abstract BaseController
public class OrderController : BaseController<OrderController>
{
private readonly IOrderManager _orderManager;
public OrderController(IOrderManager orderManager)
{
_orderManager = orderManager;
}
[HttpGet]
public string Get()
{
Logger.LogInformation("Hello World!");
return "Inside the Get method of OrderController";
}
}
I have a problem with the Automapper on my website and I can't find a solution.
I've created a class called AutoMapperProfile where I'd like to put all my Maps
public class AutoMapperProfile: Profile
{
private readonly IConfiguration _mapper;
public AutoMapperProfile(IConfiguration mapper)
{
_mapper = mapper;
}
protected override void Configure()
{
base.Configure();
_mapper.CreateMap<SlideDTO, Slide>();
_mapper.CreateMap<Slide, SlideDTO>();
}
}
For DI purposes I'm using Ninject, so I've added the following bindings in NinjectWebCommon:
kernel.Bind<IMappingEngine>().ToMethod(ctx => Mapper.Engine);
kernel.Bind<IConfigurationProvider>().ToMethod(x => Mapper.Engine.ConfigurationProvider);
The controller looks like this:
private readonly ISlideRepository slideRepository;
private readonly IMappingEngine mappingEngine;
public HomeController(
ISlideRepository slideRepository,
IMappingEngine mappingEngine)
{
this.slideRepository = slideRepository;
this.mappingEngine = mappingEngine;
}
[HttpGet]
public ActionResult Index()
{
var model = new IndexViewModel();
var slide = slideRepository.GetSlide();
model.Slide = mappingEngine.Map<SlideDTO, Slide>(slide);
return View(model);
}
When I map from SlideDTO to Slide I get the following error:
Missing type map configuration or unsupported mapping.
So my best guess is that I didn't do the binds correctly so that Automapper can see my maps, but I'm not sure how can I fix it.
You don't need to inject IConfiguration into AutoMapperProfile, it already inherits a CreateMap method from Profile.
Make sure that AutoMapperProfile has a parameterless constructor like this:
public class AutoMapperProfile : Profile
{
protected override void Configure()
{
this.CreateMap<SlideDTO, Slide>();
this.CreateMap<Slide, SlideDTO>();
}
}
And then you need to make sure that AutoMapper knows about this profile, here is how you can do it:
Mapper.Engine.ConfigurationProvider.AddProfile<AutoMapperProfile>();
Please note that you can invoke the AddProfile method on any IConfigurationProvider (if you decide not to use the global ConfigurationProvider and Engine).
I'm using SpringBoot and I am trying to create a service layer for my web application but i cant make it work.
My classes look like this
ServiceFactory
#Service
public class ServiceFactory {
#Autowired
public static EncuestaService getEncuestaService()
{
return new EncuestaServiceImpl();
}
}
EncuestaService
public interface EncuestaService {
void crearEncuesta(Encuesta encuesta, Map<String,String> parametros);
}
EncuestaServiceImpl
#Service
public class EncuestaServiceImpl implements EncuestaService {
#Override
public void crearEncuesta(Encuesta encuesta, Map<String, String> parametros) {
CrearEncuesta nueva = new CrearEncuesta(encuesta,parametros);
nueva.execute();
}
}
CrearEncuesta
#Service
public class CrearEncuesta {
private Encuesta encuesta;
private Map<String,String> parametros;
#Autowired
private RespuestasRepository respuestasRepository;
#Autowired
private EncuestasRepository encuestasRepository;
public CrearEncuesta(Encuesta encuesta, Map<String,String> parametros) {
super();
this.encuesta = encuesta;
this.parametros = parametros;
}
public void execute()
{
encuestasRepository.save(encuesta);
}
}
Everytime I call ServiceFactory.getEncuestasService().crearEncuesta() from any Controller it returns me a NullPointerException.
From what I have been reading I should not be creating a new EncuestsaServiceImpl() in my ServiceFactory but I don't really know the correct way to do so. I would appreciate if anyone could help me out :P.
Edit:
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Controller
#Controller
public class EncuestaController {
#RequestMapping(value ="registrarEncuesta", method = RequestMethod.POST)
private String formularioEncuesta(#Valid #ModelAttribute("formEncuesta") EncuestaForm formEncuesta, BindingResult bindingResult,#RequestParam Map<String,String> allRequestParams)
{
if (bindingResult.hasErrors()) {
return "nuevaEncuesta";
}
try {
Encuesta nueva = formEncuesta.toEncuesta();
ServiceFactory.getEncuestaService().crearEncuesta(nueva,allRequestParams);
} catch (DataIntegrityViolationException e) {
return "nuevaEncuesta";
}
return "redirect:/encuestas";
}
}
You will have to read a little bit more about dependency injection. The central principle in Spring Framework is dependency injection which should be used to avoid referencing beans (service implementations, repository implementations etc...) statically. Spring container also servers as a bean factory that will instantiate and inject (autowire) implementations to beans that need them.
Because Spring will instantiate service interface implementations for you, you don't need ServiceFactory. In your controller you need to add a reference (a field) to EncuestaService and annotate it as Autowired and Spring will wire in the implementation. And then you can just use it in your controller.
#Controller
public class EncuestaController {
#Autowired
EncuestaService encuestaService;
#RequestMapping(value ="registrarEncuesta", method = RequestMethod.POST)
private String formularioEncuesta(#Valid #ModelAttribute("formEncuesta") EncuestaForm formEncuesta, BindingResult bindingResult,#RequestParam Map<String,String> allRequestParams)
{
if (bindingResult.hasErrors()) {
return "nuevaEncuesta";
}
try {
Encuesta nueva = formEncuesta.toEncuesta();
encuestaService.crearEncuesta(nueva,allRequestParams);
} catch (DataIntegrityViolationException e) {
return "nuevaEncuesta";
}
return "redirect:/encuestas";
}
}
I am newbie to MVC3 application development, currently, we need following Application technologies as requirement
MVC3 framework
IOC framework – Autofac to manage object creation dynamically
Moq – Unit testing
Entity Framework
Repository and Unit Of Work Pattern of Model class
I have gone through many article to explore an basic idea about the above points but still I am little bit confused on the “Repository and Unit Of Work Pattern “. Basically what I understand Unit Of Work is a pattern which will be followed along with Repository Pattern in order to share the single DB Context among all Repository object, So here is my design :
IUnitOfWork.cs
public interface IUnitOfWork : IDisposable
{
IPermitRepository Permit_Repository{ get; }
IRebateRepository Rebate_Repository { get; }
IBuildingTypeRepository BuildingType_Repository { get; }
IEEProjectRepository EEProject_Repository { get; }
IRebateLookupRepository RebateLookup_Repository { get; }
IEEProjectTypeRepository EEProjectType_Repository { get; }
void Save();
}
UnitOfWork.cs
public class UnitOfWork : IUnitOfWork
{
#region Private Members
private readonly CEEPMSEntities context = new CEEPMSEntities();
private IPermitRepository permit_Repository;
private IRebateRepository rebate_Repository;
private IBuildingTypeRepository buildingType_Repository;
private IEEProjectRepository eeProject_Repository;
private IRebateLookupRepository rebateLookup_Repository;
private IEEProjectTypeRepository eeProjectType_Repository;
#endregion
#region IUnitOfWork Implemenation
public IPermitRepository Permit_Repository
{
get
{
if (this.permit_Repository == null)
{
this.permit_Repository = new PermitRepository(context);
}
return permit_Repository;
}
}
public IRebateRepository Rebate_Repository
{
get
{
if (this.rebate_Repository == null)
{
this.rebate_Repository = new RebateRepository(context);
}
return rebate_Repository;
}
}
}
PermitRepository .cs
public class PermitRepository : IPermitRepository
{
#region Private Members
private CEEPMSEntities objectContext = null;
private IObjectSet<Permit> objectSet = null;
#endregion
#region Constructors
public PermitRepository()
{
}
public PermitRepository(CEEPMSEntities _objectContext)
{
this.objectContext = _objectContext;
this.objectSet = objectContext.CreateObjectSet<Permit>();
}
#endregion
public IEnumerable<RebateViewModel> GetRebatesByPermitId(int _permitId)
{
// need to implment
}
}
PermitController .cs
public class PermitController : Controller
{
#region Private Members
IUnitOfWork CEEPMSContext = null;
#endregion
#region Constructors
public PermitController(IUnitOfWork _CEEPMSContext)
{
if (_CEEPMSContext == null)
{
throw new ArgumentNullException("Object can not be null");
}
CEEPMSContext = _CEEPMSContext;
}
#endregion
}
So here I am wondering how to generate a new Repository for example “TestRepository.cs” using same pattern where I can create more then one Repository object like
RebateRepository rebateRepo = new RebateRepository ()
AddressRepository addressRepo = new AddressRepository()
because , what ever Repository object I want to create I need an object of UnitOfWork first as implmented in the PermitController class. So if I would follow the same in each individual Repository class that would again break the priciple of Unit Of Work and create multiple instance of object context.
So any idea or suggestion will be highly appreciated.
Thank you
Your IUnitOfWork interface has too many responsibilities. Each time you add a new repository, you would need to change your IUnitOfWork interface and all of its implementations.
Instead, how about something like this?
public interface IUnitOfWork
{
int SaveChanges();
}
You can then implement this interface in your Entity Framework ObjectContext or DbContext:
public MyCustomContext : DbContext, IUnitOfWork
{
// ... this class already implements the SaveChanges method
}
You can then constructor inject the unit of work into each of your repositories. AutoFac can make certain the same instance is shared among multiple repositories used within the same HttpContext:
public class PermitRepository : IPermitRepository
{
#region Private Members
private readonly IObjectSet<Permit> _objectSet;
private readonly IUnitOfWork _unitOfWork;
#endregion
#region Constructors
public PermitRepository(IUnitOfWork unitOfWork, IObjectSet<Permit> objectSet)
{
_unitOfWork = unitOfWork;
_objectSet = objectSet;
}
#endregion
public IEnumerable<RebateViewModel> GetRebatesByPermitId(int _permitId)
{
// need to implment
}
}
Then, when you constructor inject the repository into the controller, the IUnitOfWork will automatically be constructor injected into the repository.
public class PermitController : Controller
{
#region Private Members
private readonly IPermitRepository _permitRepos;
#endregion
#region Constructors
public PermitController(IPermitRepository permitRepos)
{
if (permitRepos== null)
{
throw new ArgumentNullException("permitRepos");
}
_permitRepos = permitRepos;
}
#endregion
}
Note that when querying data out of your repository, you're really not doing any work, so the IUnitOfWork interface does not apply here. It applies when inserting, updating, and deleting entities, not when selecting them.