使用 Action 和 Lambda 进行 NUnit 测试
NUnit Testing with Action and Lambda
我一直在阅读 MSDN 上的 Actions 和 Lambda 表达式,但仍然缺少一些东西。我有以下 public class.
public class ExitChecker
{
public Action EnvironmentExitAction { get; set; }
public ExitChecker(string[] args)
{
if (string.Compare(args[0], "-help", true) == 0)
{
EnvironmentExitAction = () => Environment.Exit(0);
}
}
}
我有以下测试class。
[TestFixture]
public class AVSRunnerConsoleAppTests
{
[Test]
public void TestConsoleAppWithHelpArg()
{
string[] args = new string[1] { "-help" };
ExitChecker exitchecker = new ExitChecker(args);
bool exitZeroOccured = false;
exitchecker.EnvironmentExitAction = () => exitZeroOccured = true;
Assert.That(exitZeroOccured, Is.True);
}
}
我正在尝试测试 Environment.Exit 而不实际调用 Environment.Exit。一切似乎都可以编译并且 运行 很好,但我似乎无法将 Lambda 表达式中的 exitZeroOccured 更改为 true。有人能指出我正确的方向吗?
您永远不会调用 EnvironmentExitAction
。将您的代码更改为:
[Test]
public void TestConsoleAppWithHelpArg()
{
string[] args = new string[1] { "-help" };
ExitChecker exitchecker = new ExitChecker(args);
bool exitZeroOccured = false;
exitchecker.EnvironmentExitAction = () => exitZeroOccured = true;
exitchecker.EnvironmentExitAction.Invoke();
Assert.That(exitZeroOccured, Is.True);
}
我一直在阅读 MSDN 上的 Actions 和 Lambda 表达式,但仍然缺少一些东西。我有以下 public class.
public class ExitChecker
{
public Action EnvironmentExitAction { get; set; }
public ExitChecker(string[] args)
{
if (string.Compare(args[0], "-help", true) == 0)
{
EnvironmentExitAction = () => Environment.Exit(0);
}
}
}
我有以下测试class。
[TestFixture]
public class AVSRunnerConsoleAppTests
{
[Test]
public void TestConsoleAppWithHelpArg()
{
string[] args = new string[1] { "-help" };
ExitChecker exitchecker = new ExitChecker(args);
bool exitZeroOccured = false;
exitchecker.EnvironmentExitAction = () => exitZeroOccured = true;
Assert.That(exitZeroOccured, Is.True);
}
}
我正在尝试测试 Environment.Exit 而不实际调用 Environment.Exit。一切似乎都可以编译并且 运行 很好,但我似乎无法将 Lambda 表达式中的 exitZeroOccured 更改为 true。有人能指出我正确的方向吗?
您永远不会调用 EnvironmentExitAction
。将您的代码更改为:
[Test]
public void TestConsoleAppWithHelpArg()
{
string[] args = new string[1] { "-help" };
ExitChecker exitchecker = new ExitChecker(args);
bool exitZeroOccured = false;
exitchecker.EnvironmentExitAction = () => exitZeroOccured = true;
exitchecker.EnvironmentExitAction.Invoke();
Assert.That(exitZeroOccured, Is.True);
}