문제

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