{"action":"closed","issue":{"url":"https://api.github.com/repos/nsubstitute/NSubstitute/issues/467","repository_url":"https://api.github.com/repos/nsubstitute/NSubstitute","labels_url":"https://api.github.com/repos/nsubstitute/NSubstitute/issues/467/labels{/name}","comments_url":"https://api.github.com/repos/nsubstitute/NSubstitute/issues/467/comments","events_url":"https://api.github.com/repos/nsubstitute/NSubstitute/issues/467/events","html_url":"https://github.com/nsubstitute/NSubstitute/issues/467","id":371742033,"node_id":"MDU6SXNzdWUzNzE3NDIwMzM=","number":467,"title":"Calling When on partial substitute fails for async methods","user":{"login":"siewers","id":226147,"node_id":"MDQ6VXNlcjIyNjE0Nw==","avatar_url":"https://avatars1.githubusercontent.com/u/226147?v=4","gravatar_id":"","url":"https://api.github.com/users/siewers","html_url":"https://github.com/siewers","followers_url":"https://api.github.com/users/siewers/followers","following_url":"https://api.github.com/users/siewers/following{/other_user}","gists_url":"https://api.github.com/users/siewers/gists{/gist_id}","starred_url":"https://api.github.com/users/siewers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/siewers/subscriptions","organizations_url":"https://api.github.com/users/siewers/orgs","repos_url":"https://api.github.com/users/siewers/repos","events_url":"https://api.github.com/users/siewers/events{/privacy}","received_events_url":"https://api.github.com/users/siewers/received_events","type":"User","site_admin":false},"labels":[{"id":51618,"node_id":"MDU6TGFiZWw1MTYxOA==","url":"https://api.github.com/repos/nsubstitute/NSubstitute/labels/bug","name":"bug","color":"ff0000","default":true}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-10-18T22:44:47Z","updated_at":"2018-10-20T08:39:59Z","closed_at":"2018-10-20T08:39:59Z","author_association":"NONE","body":"**Describe the bug**\r\nReSharper test runner fails executing the test, when calling `When` on a partial substitute.\r\n\r\nError output:\r\n```cs\r\n2018.10.19 00:32:26.157    WARN The active test run was aborted. Reason: \r\n2018.10.19 00:32:27.884   ERROR Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. at System.Net.Sockets.NetworkStream.Read(Span`1 destination) at System.Net.Sockets.NetworkStream.ReadByte() at System.IO.BinaryReader.ReadByte() at System.IO.BinaryReader.Read7BitEncodedInt() at System.IO.BinaryReader.ReadString() at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.LengthPrefixCommunicationChannel.NotifyDataAvailable() at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TcpClientExtensions.MessageLoopAsync(TcpClient client, ICommunicationChannel channel, Action`1 errorHandler, CancellationToken cancellationToken) Source: System.Net.Sockets HResult: -2146232800 Inner Exception: An existing connection was forcibly closed by the remote host HResult: -2147467259\r\n\r\n--- EXCEPTION #1/1 [LoggerException]\r\nMessage = “\r\n  Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.\r\n     at System.Net.Sockets.NetworkStream.Read(Span`1 destination)\r\n     at System.Net.Sockets.NetworkStream.ReadByte()\r\n     at System.IO.BinaryReader.ReadByte()\r\n     at System.IO.BinaryReader.Read7BitEncodedInt()\r\n     at System.IO.BinaryReader.ReadString()\r\n     at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.LengthPrefixCommunicationChannel.NotifyDataAvailable()\r\n     at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.TcpClientExtensions.MessageLoopAsync(TcpClient client, ICommunicationChannel channel, Action`1 errorHandler, CancellationToken cancellationToken)\r\n  Source: System.Net.Sockets\r\n  HResult: -2146232800\r\n  \r\n  Inner Exception: An existing connection was forcibly closed by the remote host\r\n  HResult: -2147467259\r\n”\r\nExceptionPath = Root\r\nClassName = JetBrains.Util.LoggerException\r\nHResult = COR_E_APPLICATION=80131600\r\nStackTraceString = “\r\n  at JetBrains.ReSharper.UnitTestFramework.DotNetCore.DotNetVsTest.DotNetVsTestExecution.OnRunComplete(ExecutionCompletePayload payload)\r\n     at Appccelerate.StateMachine.Machine.ActionHolders.ArgumentActionHolder`1.Execute(Object argument)\r\n     at Appccelerate.StateMachine.Machine.Transitions.Transition`2.PerformActions(ITransitionContext`2 context)\r\n     at Appccelerate.StateMachine.Machine.Transitions.Transition`2.Fire(IState`2 source, IState`2 target, ITransitionContext`2 context)\r\n     at Appccelerate.StateMachine.Machine.Transitions.Transition`2.Fire(IState`2 source, IState`2 target, ITransitionContext`2 context)\r\n     at Appccelerate.StateMachine.Machine.Transitions.Transition`2.Fire(IState`2 source, IState`2 target, ITransitionContext`2 context)\r\n     at Appccelerate.StateMachine.Machine.Transitions.Transition`2.Fire(ITransitionContext`2 context)\r\n     at Appccelerate.StateMachine.Machine.States.State`2.Fire(ITransitionContext`2 context)\r\n     at Appccelerate.StateMachine.Machine.StateMachine`2.Fire(TEvent eventId, Object eventArgument)\r\n     at Appccelerate.StateMachine.ActiveStateMachine`2.ProcessEventQueue(CancellationToken cancellationToken)\r\n     at Appccelerate.StateMachine.ActiveStateMachine`2.<Start>b__32_0()\r\n     at System.Threading.Tasks.Task.InnerInvoke()\r\n     at System.Threading.Tasks.Task.Execute()\r\n     at System.Threading.Tasks.Task.ExecutionContextCallback(Object obj)\r\n     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n     at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)\r\n     at System.Threading.Tasks.Task.ExecuteEntry(Boolean bPreventDoubleExecution)\r\n     at System.Threading.Tasks.ThreadPoolTaskScheduler.LongRunningThreadWork(Object obj)\r\n     at System.Threading.ThreadHelper.ThreadStart_Context(Object state)\r\n     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)\r\n     at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n     at System.Threading.ThreadHelper.ThreadStart(Object obj)\r\n”\r\n```\r\n\r\n**To Reproduce**\r\n```cs\r\n[TestFixture]\r\npublic class StuffDoerTests\r\n{\r\n    [Test]\r\n    public async Task CanDoStuff()\r\n    {\r\n        var stuffDoer = Substitute.ForPartsOf<StuffDoer>();\r\n        // This does not work\r\n        stuffDoer.When(async s => await s.DoStuff(Arg.Any<string>())).DoNotCallBase();\r\n        // This works but produces a compiler warning: CS4014: Async method invocation without await expression\r\n        // stuffDoer.When(s => s.DoStuff(Arg.Any<string>())).DoNotCallBase();\r\n        stuffDoer.DoStuff(Arg.Any<string>()).Returns(\"StuffValue\");\r\n\r\n        string stuff = await stuffDoer.GetStuff(\"Input\");\r\n\r\n        Assert.That(stuff, Is.EqualTo(\"StuffValueInput\"));\r\n    }\r\n}\r\n\r\npublic class StuffDoer\r\n{\r\n    public async Task<string> GetStuff(string input)\r\n    {\r\n        string stuff = await DoStuff(input);\r\n        return stuff + input;\r\n    }\r\n\r\n    public virtual async Task<string> DoStuff(string value)\r\n    {\r\n        return await Task.Run(() => value.GetHashCode().ToString());\r\n    }\r\n}\r\n```\r\n**Expected behaviour**\r\nI would expect the test to pass but it fails.\r\n\r\n**Environment:**\r\n - NSubstitute version: [e.g. 3.1.0.130] (pre-release). Stable release exhibits same behavior.\r\n - Platform: dotnetcore2.0 project on Windows 10\r\n - Test runner: ReSharper 2018.2.3\r\n - Visual Studio 2017 15.8.7\r\n\r\n"}}
{"actor":{"display_login":"dtchepak"}}