用mvc4寫一個Ninject測試

[英]Write a Ninject test with mvc4


I am really new to ninject and testing too.

我對ninject和測試也很陌生。

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:) )

我有一個mvc4應用程序,我想開始編寫測試。 (是的,我知道我打算在我去的時候寫它們:))

Within my NinjectWebCommon

在我的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

對我來說,編寫測試以檢查是否從Web應用程序存儲庫返回ConsumerModel的最佳方法是什么?我希望它具有靈活性,因為我想編寫更復雜的測試並提供模擬的ConsumerModel,而不是每次都像上面那樣調用api

Thank you for any suggestions!

謝謝你的任何建議!

1 个解决方案

#1


0  

The MVCContrib TestHelper might help. It'll help you unit test your controller actions and will also initialise all those parts like HttpContext and Session:

MVCContrib TestHelper可能有所幫助。它將幫助您對控制器操作進行單元測試,並且還會初始化所有這些部分,如HttpContext和Session:

http://mvccontrib.codeplex.com/wikipage?title=TestHelper


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/10/15/596bd7384cdcfd7794cd03a961deecaa.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com