Search code examples
c#asp.netnunittestcase

Testcases using Nunit with global variables


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?


Solution

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