惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
J
Java Code Geeks
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
MyScale Blog
MyScale Blog
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
N
Netflix TechBlog - Medium
MongoDB | Blog
MongoDB | Blog
I
InfoQ
WordPress大学
WordPress大学
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security

博客园 - 李昀璟

Export to Excel, Word with css (Cascading Style Sheets) IIS 不能运行asp.net Useful Expressions - Business Correspondence How to Plan a Meeting Useful Expressions for Business Interaction Useful Expressions for Business Language 使用IP或机器名而不是localhost访问DotNetNuke DotNetNuke升级中遇到的问题 DNN端口的问题 升级DotNetNuke DotNetNuke的升级路径 设置为自动启动的WindowService没有开机启动 检测是否连网 C# WinForm 边框阴影窗体 PostSubmitter~在WEB应用程序以外的其他程序里提交Web请求的类 Asp.Net部署问题 MSDTC的折磨 - 李昀璟 - 博客园 常用缩写 日本語文法勉強
Unit Tests for ASP.NET MVC application that uses resource...
李昀璟 · 2012-08-14 · via 博客园 - 李昀璟

Some time ago I start to play with ASP.NET MVC. It is very well pattern and Microsoft made nice choose that implemented it for ASP.NET in my opinion.

I started a project that uses ASP.NET MVC. This project requests multilingual support. I use resources for this. It works well when I use resource's properties in the View's layer but when I call resource's properties from code behind. It's something likes to next:

public ActionResult Index()

{

ViewData["Message"] = CommonResource.WELCOME;

return View();

}

It works but I pay attention that this code breaks my Unit Tests. My Unit Tests looks like:

[TestMethod]

public void Index_Test()

{

//// Arrange

var controller = new HomeController();

// Act

var result = controller.Index();

// Assert

Assert.IsInstanceOfType(result, typeof(ViewResult));

}

It happens because I run my code in other project and my ResourceManager is not filled (it equals to null). How can I fix it? A first idea that came to mind is "extract interface".. but it is very annoying because resource's class is auto-generate and I should update this code after each updating of resources.. I made some research and found additional way for extracting resources - HttpContext contains GetGlobalResourceObject method! It is well way because I can set mock for HttpContext and for any its methods or properties.

Note: GetGlobalResourceObject returns object but I use only strings for localization. So I create linq method for HttpContext that returns string if it presents:

public static string GetGlobalResource(

   this HttpContextBase httpContext

  , string classKey, string resourceKey)

{

var result = httpContext.GetGlobalResourceObject

                    (classKey, resourceKey);

return null != result

  ? result.ToString() : string.Empty;

}

Pay attention, MissingManifestResourceException is possible in this code if you pass incorrect key/keys!

So I change pieces of code in Controller's classes in the next manner:

public ActionResult Index()

{

ViewData[CommonStrings.MESSAGE] = HttpContext

        .GetGlobalResource(CommonStrings

            .COMMON_RESOURCE, CommonStrings.WELCOME);

return View();

}

After it I should change my Unit Test's methods in next manner:

[TestMethod]

public void Index_Test()

{

//// Arrange

var httpContextMock = MockRepository

              .GenerateMock<HttpContextBase>();

httpContextMock.Stub

   (x => x.GetGlobalResource(

      CommonStrings.COMMON_RESOURCE, CommonStrings.WELCOME))

   .Return(CommonStrings.WELCOME);

var controller = new HomeController();

var context = new ControllerContext

  (httpContextMock, new RouteData(), controller);

controller.ControllerContext = context;

// Act

var result = controller.Index();

// Assert

Assert.IsInstanceOfType(result, typeof(ViewResult));

ViewDataDictionary viewData = ((ViewResult)result).ViewData;

Assert.AreEqual(CommonStrings.WELCOME

        , viewData[CommonStrings.MESSAGE]);

}

Moreover, you can see that I added additional checking related to localization.