Im using this code in C# project
public async Task should_not_raise_exception(Mock<IWebSocketClient> webSocket, SlackConnection slackConnection)
{
// given
var connectionInfo = new ConnectionInformation
{
WebSocket = webSocket.Object
};
await slackConnection.Initialise(connectionInfo);
var inboundMessage = new ChatMessage
{
MessageType = MessageType.Message,
User = "lalala"
};
slackConnection.OnMessageReceived += message => throw new Exception("EMPORER OF THE WORLD");
// when & then
Assert.DoesNotThrow(() => webSocket.Raise(x => x.OnMessage += null, null, inboundMessage));
}
But when I compile it, this shows me an error about ; expected, the code part about error is this:
slackConnection.OnMessageReceived += message => throw new Exception("EMPORER OF THE WORLD");
// when & then
Assert.DoesNotThrow(() => webSocket.Raise(x => x.OnMessage += null, null, inboundMessage));
I'm using framework 4.6.1
Check the attached image
You can reproduce the problem with just this line of code:
Action<string> foo = message => throw new Exception();
The problem is that before C# 7.0, you couldn't use throw
as an expression on its own... and that's what an expression-bodied lambda expression has to have.
That line of code is valid in C# 7, but not in C# 6 or earlier. It doesn't matter which version of the framework you're using - it matters which C# compiler you're using.
You can fix your code just by using a statement lambda (basically, a lambda expression that uses braces) instead:
slackConnection.OnMessageReceived +=
message => { throw new Exception("EMPORER OF THE WORLD"); };
... or you could upgrade to use C# 7 :) (For example, by updating to VS 2017.)
See more on this question at Stackoverflow