use*_*595 3 .net c# unit-testing visual-studio-2012
嗨我有一个简单的方法如下,需要知道如何在Visual Studio中的单元测试中调用它
public class myClass
{
public static bool Test(string value, string regex)
{
if (Regex.IsMatch(value, regex, RegexOptions.IgnoreCase))
return true;
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我所熟悉的
[TestMethod]
public void TestMethod_Test()
{
string value = "myValue";
string regex = "&#@<>\s\\\$\(\)";
PrivateType pt = new PrivateType(typeof(myClass));
bool actualresult = (bool)pt.InvokeStatic("Test", new object[] { value, regex });
bool expectedResult = false;
Assert.AreEqual(actualresult, expectedResult);
}
Run Code Online (Sandbox Code Playgroud)
你不想使用反射.只需直接调用该方法:
[TestMethod]
public void TestMethod_Test()
{
string value = "myValue";
string regex = "&#@<>\s\\\$\(\)";
var result = ClassContainingTest.Test(value, regex);
Assert.AreEqual(false, result);
}
Run Code Online (Sandbox Code Playgroud)
如果ClassContainingTest不公开,那么尝试进行单元测试是不明智的Test.仅测试可公开访问的功能.