I have created a asp.net 4.0 web application using c#. I have a class in which I have used global variables. I have set the value of this variable in Page_Load method and accessing in other functions.

Now I want to create test case of that function. I want to know that how can I set the value of that global variable before calling to that function in test case using nunit.

Here is my code:

public string userId = "";   
protected void Page_Load(object sender, EventArgs e)  
{  
    userId = Membership.GetUser().ProviderUserKey.ToString();

//some code here

}

public bool IsEntryExist()  
{   
   string query="SELECT COUNT(*) FROM table WHERE user_id = '"+userId+"'";    

   bool Exist = Convert.ToBoolean(db.SelectScaler(query));   
}

Here I have created a test case in nunit.

[TestFixture]
public class Testing
{
   [Test]
   public void TestUser()
   {
      Assert.IsTrue(IsEntryExist())
   }
}

How can I set the value of UserId?

有帮助吗?

解决方案

[Test]
public void TestUser()
{
    MyClass myClass = new MyClass();
    MyClass.userid = "test value";
    Assert.IsTrue(IsEntryExist())
}

其他提示

First you are not using global variables. You are using member variable.

Second it would be probably better to move this code to some other class that would contain method

public bool IsEntryExist(int id)

Than it would be easier to test it. And you wouldn't need to instantiate ASP.NET Page class in unit tests which is a bit weird.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top