I'm writing some unit tests. Taken straight from NUnit's documentation, I should be able to do something like this:
Assert.That( SomeMethod, Throws.ArgumentException );
In a test method, this won't compile:
Assert.That(()=>viewModel.SaveCommand_Exec(this, new ExecutedRoutedEventArgs()),
Throws.Nothing);
// Compiler error:
// Cannot convert lambda expression to type 'bool'
// because it is not a delegate type
But this will:
ExecutedRoutedEventArgs e = new ExecutedRoutedEventArgs();
Assert.That(() => viewModel.SaveCommand_Exec(this, e), Throws.Nothing);
And so will this:
Assert.DoesNotThrow(()=>viewModel.SaveCommand_Exec(this,
new ExecutedRoutedEventArgs()));
Obviously, I've got two workarounds, but I'm struggling a little bit to understand what's going on here. Why can I new up an ExecutedRoutedEventArgs in the lambda that creates one delegate parameter but not the other.
More intriguingly, what exactly is the difference in creating the EventArgs object outside the lambda, instead of inside it? I realise this creates a closure, but I don't understand how that changes the signature of the anonymous method.
I'm using VS2013, targetting .net 4.0
I don't have access to the exact classes you're using in your example, but the following very similar code compiles and runs fine for me in VS 2013 targeting .NET 4. Are you using NUnit 2.6.3? Also, you correctly identified the only difference between creating the ExecutedRoutedEventArgs instance outside the lambda as opposed to inside it: a closure.
using NUnit.Framework;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main()
{
var viewModel = new ViewModel();
Assert.That(() => viewModel.SaveCommand_Exec(null, new ExecutedRoutedEventArgs()), Throws.Nothing);
}
}
public class ViewModel
{
public bool SaveCommand_Exec(object sender, ExecutedRoutedEventArgs e)
{
return true;
}
}
public class ExecutedRoutedEventArgs
{
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With