用于测试缓存数据的 Nunit 脚本

发布于 2024-11-19 13:47:39 字数 1842 浏览 3 评论 0原文

我正在尝试编写一个示例 NUnit 测试脚本来检查缓存中的值

,我编写了如下代码

 [TestFixture]

class Authorization
{
    class AutherizationEntity
    {
        public int UserID { get; set; }          
        public int OperationCode { get; set; }
        public bool permission { get; set; }
    }


    [SetUp]
    public void Initialize()
    {

            //if (HttpContext.Current.Cache["UserRights"] == null) 
            //{
                List<AutherizationEntity> AuthorisationObject = new List<AutherizationEntity>();

                for (int i = 0; i < 5; i++)
                {
                    AutherizationEntity AEntity = new AutherizationEntity();
                    AEntity.OperationCode = 10;
                    AEntity.permission = true;
                    AEntity.UserID = i;
                    AuthorisationObject.Add(AEntity);
                }
                HttpContext.Current.Cache.Insert("UserRights", AuthorisationObject);  //Here i am getting the exception in NUnit
            //}

    }

    [TestCase]
    public void AuthorizeUser()
    {           
        int UserId = 1;
        int OperationCode = 10;        
        Boolean HaveRight = false;

        List<AutherizationEntity> AuthEntity = new List<AutherizationEntity>();
        AuthEntity = (List<AutherizationEntity>)HttpContext.Current.Cache.Get("UserRights");

        foreach (AutherizationEntity Auth in AuthEntity)
        {
            if ((Auth.UserID == UserId) && (Auth.OperationCode==OperationCode))
            {
                HaveRight = Auth.permission;
            }
        }

        Assert.AreEqual(HaveRight, true);
    }     

}

,但是当我尝试使用 NUnit 运行脚本时,我收到异常

Authorization.AuthorizeUser(): SetUp:System.NullReferenceException:未将对象引用设置为对象的实例。

你能帮我吗?

I am trying write a sample NUnit test script to check the value in cache

I wrote the code like

 [TestFixture]

class Authorization
{
    class AutherizationEntity
    {
        public int UserID { get; set; }          
        public int OperationCode { get; set; }
        public bool permission { get; set; }
    }


    [SetUp]
    public void Initialize()
    {

            //if (HttpContext.Current.Cache["UserRights"] == null) 
            //{
                List<AutherizationEntity> AuthorisationObject = new List<AutherizationEntity>();

                for (int i = 0; i < 5; i++)
                {
                    AutherizationEntity AEntity = new AutherizationEntity();
                    AEntity.OperationCode = 10;
                    AEntity.permission = true;
                    AEntity.UserID = i;
                    AuthorisationObject.Add(AEntity);
                }
                HttpContext.Current.Cache.Insert("UserRights", AuthorisationObject);  //Here i am getting the exception in NUnit
            //}

    }

    [TestCase]
    public void AuthorizeUser()
    {           
        int UserId = 1;
        int OperationCode = 10;        
        Boolean HaveRight = false;

        List<AutherizationEntity> AuthEntity = new List<AutherizationEntity>();
        AuthEntity = (List<AutherizationEntity>)HttpContext.Current.Cache.Get("UserRights");

        foreach (AutherizationEntity Auth in AuthEntity)
        {
            if ((Auth.UserID == UserId) && (Auth.OperationCode==OperationCode))
            {
                HaveRight = Auth.permission;
            }
        }

        Assert.AreEqual(HaveRight, true);
    }     

}

But when i am trying to run the script with NUnit i am getting an exception

Authorization.AuthorizeUser():
SetUp : System.NullReferenceException : Object reference not set to an instance of an object.

can you please help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红衣飘飘貌似仙 2024-11-26 13:47:39

NUnit 不在 Web 上下文中运行,因此 HttpContext.Current 为 null。如果您想测试它,您需要手动创建一个上下文。

您可以为您的 httpcontext 创建一个模拟类。请 Google mock httpcontext,您会发现几个可能对您有用的链接。

NUnit doesn't run on a web context so HttpContext.Current is null. You'll need to create a we context manually if you want to test that.

You could create a mock class for your httpcontext. Please Google mock httpcontext and you'll find several links that may be useful for you.

怪我闹别瞎闹 2024-11-26 13:47:39

当测试绑定到 HttpContext 的代码时,您必须依赖抽象并使用假/存根 http 上下文。请参阅此问题了解有关该主题的更多信息。

不幸的是,缓存对象(和 Cache 类)不可能轻易伪造,因为它是一个密封类。解决方法是创建一个包装类,在测试中围绕 Cache 对象和 fake/stub/mock 提供相应的接口。

您可以在 Web 应用程序外部访问 Cache 类,但在这种情况下您可能不想这样做。可以通过引用 System.Web 程序集并使用 HttpRuntime 类来访问缓存。

using System.Web;
using System.Web.Caching;
…
Cache cache = HttpRuntime.Cache;

When testing code bound towards the HttpContext you have to rely upon abstractions and use a fake/stubbed http context. See this question for more information on the topic.

Unfortunately the cache object (and the Cache class) is not possible to fake easily, since it's a sealed class. The way to go is to create a wrapper class with a corresponding interface around the Cache object and fake/stub/mock that in your test.

You can access the Cache class outside a web application, but in this case you'd probably not want to do that. Accessing the cache can be done by referencing the System.Web assembly and using the HttpRuntime class.

using System.Web;
using System.Web.Caching;
…
Cache cache = HttpRuntime.Cache;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文