Reputation: 8183
I am using Fluent MVC to test some of my MVC controllers (some people argue this is not needed but this is more for a learning process rather than a production product).
Interface
public interface ISettings<T> where T : BaseSettingsDto
{
T GetSettings();
bool SaveSettings(T model);
}
Implementing the interface
public class GetSettingsConfiguration : ISettings<FirstSettingsDto>
{
public FirstSettingsDto GetSettings()
{
var repo = new Repository();
var result = repo.GetAll();
// Do stuff
return model;
}
public bool SaveSettings(FirstSettingsDto model)
{
var repo = new Repository();
// Do stuff
}
}
How I am using this in my controller (example):
[HttpGet]
public ActionResult GetInformation()
{
var admin = new GetSettingsConfiguration();
var config = admin.GetSettings();
}
How would I actually mock this out since the Interface is using generics?
Upvotes: 2
Views: 67
Reputation: 560
You can save ISettings as controller class field and init it from constructor parameter.
public Controller
{
private ISettings<FirstSettingsDto> _admin;
public Controller(ISettings<FirstSettingsDto> admin = null)
{
_admin = admin ?? new GetSettingsConfiguration();
}
[HttpGet]
public ActionResult GetInformation()
{
var config = _admin.GetSettings();
}
}
Now you can create Controller with ISettings mock.
UPDATE: set default value for ctor parameter (thx @JB06)
Upvotes: 2