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

推荐订阅源

T
Threat Research - Cisco Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
GbyAI
GbyAI
P
Proofpoint News Feed
L
LINUX DO - 热门话题
P
Palo Alto Networks Blog
A
About on SuperTechFans
T
Tenable Blog
M
MIT News - Artificial intelligence
IT之家
IT之家
I
Intezer
D
DataBreaches.Net
爱范儿
爱范儿
T
Threatpost
C
CERT Recently Published Vulnerability Notes
云风的 BLOG
云风的 BLOG
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
K
Kaspersky official blog
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
Y
Y Combinator Blog
Cyberwarzone
Cyberwarzone
酷 壳 – CoolShell
酷 壳 – CoolShell
D
Darknet – Hacking Tools, Hacker News & Cyber Security
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
AWS News Blog
AWS News Blog
博客园 - 聂微东
C
Check Point Blog
S
Securelist
有赞技术团队
有赞技术团队
雷峰网
雷峰网
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
MongoDB | Blog
MongoDB | Blog
D
Docker
G
GRAHAM CLULEY
T
The Exploit Database - CXSecurity.com
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tailwind CSS Blog
L
Lohrmann on Cybersecurity
G
Google Developers Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LangChain Blog

博客园 - 李昀璟

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 resources in code behind.
李昀璟 · 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.