简体   繁体   English

ASP.NET MVC中的单元测试应用程序变量

[英]Unit testing application variables in asp.net mvc

Hi I am having trouble unit testing my application that use application variables. 嗨,我在使用应用程序变量对应用程序进行单元测试时遇到麻烦。 Below is my test method. 下面是我的测试方法。 accController.Clist(cIn) calls methods that use application variables that are read in from the global.asax file. accController.Clist(cIn)调用使用从global.asax文件中读取的应用程序变量的方法。 How and where do I declare it so that the application variables are actually used. 我如何以及在哪里声明它,以便实际使用应用程序变量。

[TestMethod]
public void TestGetCompanyList()
{
    var accController = new AccSerController();
    CInt cInt = new CInt();
    cIn.Iss = "Other";
    cIn.Tick = "BK";
    var result = accController.GetClist(cIn) as IEnumerable<CList>;
    Assert.IsNotNull(result);
}

I know that mocking is used, but I am not that experienced with mocking. 我知道使用了模拟,但是我对模拟没有经验。 Also I did not write the code (controllers, models, etc), so i cannot change the layout of it. 我也没有写代码(控制器,模型等),所以我无法更改其布局。

Btw I do not have any interfaces, abstract classes, or virtual methods, most other examples are working with one of those. 顺便说一句,我没有任何接口,抽象类或虚拟方法,大多数其他示例正在使用其中之一。

I'd look at using the AmbientContext pattern 我看一下使用AmbientContext模式

This basically acts as a mockable wrapper around variables that are static or otherwise difficult to access 这基本上充当了静态或难以访问的变量的可模拟包装器

It will allow you to keep your application variables declared in the global.asax, but to overwrite them at test time 它将允许您将应用程序变量保留在global.asax中声明,但在测试时将其覆盖

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM