用于测试缓存数据的 Nunit 脚本
我正在尝试编写一个示例 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
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.当测试绑定到 HttpContext 的代码时,您必须依赖抽象并使用假/存根 http 上下文。请参阅此问题了解有关该主题的更多信息。
不幸的是,缓存对象(和 Cache 类)不可能轻易伪造,因为它是一个密封类。解决方法是创建一个包装类,在测试中围绕 Cache 对象和 fake/stub/mock 提供相应的接口。
您可以在 Web 应用程序外部访问 Cache 类,但在这种情况下您可能不想这样做。可以通过引用 System.Web 程序集并使用 HttpRuntime 类来访问缓存。
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.