I have the following controller
public class ProviderController : Controller
{
private static readonly IProviderRepository _repository = new ProviderRepository();
private static readonly Ilogger Logger = new Logger();
[HttpPost]
public ActionResult CreateProvider(Provider provider)
{
try
{
int providerCreationSuccessful = _repository.CreateProvider(provider);
if (providerCreationSuccessful == 1)
TempData["userIntimation"] = "Provider Registered Successfully";
return RedirectToAction("ShowTheListOfProviders");
}
catch (Exception Ex)
{
Logger.Error(Ex.Message);
return View("Error");
}
}
}
The IProviderRepository looks like this.
public interface IProviderRepository
{
List<Provider> GetListofProviders();
Provider GetSingleProviderDetails(int ProviderID);
int CreateProvider(Provider provider);
int DeleteProvider(int ProviderID);
int UpdateProviderDetails(Provider provider);
}
I have to MOQ the controller apparently.I am guessing that means I ahve to MPOQ the methods.So,I created a test project with the following method.
public class ProviderControllerTests
{
Provider _provider;
Mock<IProviderRepository> mockProviderRepository;
[TestInitialize]
public void InitializeTestData()
{
_provider = new Provider();
mockProviderRepository = new Mock<IProviderRepository>();
}
[TestMethod()]
public void repository_CreateProviderTest()
{
//Act
mockProviderRepository.Setup(provider => provider.CreateProvider(_provider)).Returns(1);
//Assert
throw new NotImplementedException();
}
}
Could some one guide me on what exactly I need to mock here.I feel like I am a little in over my head.
You need to mock IProviderRepository which you have done.
In order not to interact with the real database you have to the following
So you can now test your Actions of the ProviderController using this mocked repository.