C# 带动作和Lambda的NUnit测试
我一直在阅读MSDN上的操作和Lambda表达式,但我仍然缺少一些东西。我有下面的公开课C# 带动作和Lambda的NUnit测试,c#,testing,lambda,nunit,action,C#,Testing,Lambda,Nunit,Action,我一直在阅读MSDN上的操作和Lambda表达式,但我仍然缺少一些东西。我有下面的公开课 public class ExitChecker { public Action EnvironmentExitAction { get; set; } public ExitChecker(string[] args) { if (string.Compare(args[0], "-help", true) == 0) {
public class ExitChecker
{
public Action EnvironmentExitAction { get; set; }
public ExitChecker(string[] args)
{
if (string.Compare(args[0], "-help", true) == 0)
{
EnvironmentExitAction = () => Environment.Exit(0);
}
}
}
我有下面的测试课
[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表达式中的exitzeroOccursed更改为true。有人能给我指出正确的方向吗 您永远不会调用
环境ExitAction
。将代码更改为:
[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);
}
哇!我真不敢相信我错过了。那就去多读点书吧……多谢了。实际上我认为调用发生在Lambda表达式本身。Pff,别担心。。。我一直都这么做。不过,老实说,你的测试并没有起到任何作用。您只是在测试调用
操作
是否有效,这显然是因为它是一个.NET类。您从未调用您的环境。退出(0)代码>代码作为整个测试代码驻留在测试本身中。哈!对。我一直在尝试一些不同的测试选项。Exit(0)终止了测试,它可能不是应该测试的东西,但我想我会尝试一下。