I am really new to ninject and testing too.
I have an mvc4 application that I want to start writing tests for. (Yes I know I am meant to write them as I go:) )
Within my NinjectWebCommon
private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
return kernel;
}
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IConsumerRepository>().To<ConsumerRepository>();
}
I have written everything in repository’s within my application for example I have
public interface IConsumerRepository
{
ConsumerModel GetConsumerByUserId(Guid userId);
}
public ConsumerModel GetConsumerByUserId(Guid userId)
{
if (HttpContext.Current.Session["ConsumerInfo"] == null)
{
var data = RestSharperHelper.GetById<ConsumerModel>("id", userId, ApiEndPointConstants.GetConsumerById);
LogMessage(data.UserName);
HttpContext.Current.Session["ConsumerInfo"] = data;
return data;
}
else
{
return (ConsumerModel)HttpContext.Current.Session["ConsumerInfo"];
}
}
Whats the best way for me to write a test to check that a ConsumerModel is returned from web applications repository? I want it be flexible as in time I want to write more complicated tests and provide a mock ConsumerModel instead of calling the api like the above everytime
Thank you for any suggestions!
The MVCContrib TestHelper might help. It’ll help you unit test your controller actions and will also initialise all those parts like
HttpContextandSession:http://mvccontrib.codeplex.com/wikipage?title=TestHelper