{"action":"closed","issue":{"url":"https://api.github.com/repos/xunit/xunit/issues/883","repository_url":"https://api.github.com/repos/xunit/xunit","labels_url":"https://api.github.com/repos/xunit/xunit/issues/883/labels{/name}","comments_url":"https://api.github.com/repos/xunit/xunit/issues/883/comments","events_url":"https://api.github.com/repos/xunit/xunit/issues/883/events","html_url":"https://github.com/xunit/xunit/issues/883","id":160754064,"number":883,"title":"Problems testing class that uses SynchronizationContext.Post","user":{"login":"brianlagunas","id":835562,"avatar_url":"https://avatars.githubusercontent.com/u/835562?v=3","gravatar_id":"","url":"https://api.github.com/users/brianlagunas","html_url":"https://github.com/brianlagunas","followers_url":"https://api.github.com/users/brianlagunas/followers","following_url":"https://api.github.com/users/brianlagunas/following{/other_user}","gists_url":"https://api.github.com/users/brianlagunas/gists{/gist_id}","starred_url":"https://api.github.com/users/brianlagunas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brianlagunas/subscriptions","organizations_url":"https://api.github.com/users/brianlagunas/orgs","repos_url":"https://api.github.com/users/brianlagunas/repos","events_url":"https://api.github.com/users/brianlagunas/events{/privacy}","received_events_url":"https://api.github.com/users/brianlagunas/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-06-16T20:32:27Z","updated_at":"2016-06-17T01:18:33Z","closed_at":"2016-06-17T01:18:33Z","body":"I am having the hardest time trying to test a ICommand class the uses the SynchronizationContext.Post method.  Every test fails because the Post hasn't completed by the time the Assert is checked.  How on earth do I do this with xUnit?\r\n\r\nScenario; the DelegateCommand class has a static property that it uses to grab the SyncContext.\r\n\r\n```\r\nprivate static readonly SynchronizationContext _synchronizationContext = SynchronizationContext.Current;\r\n```\r\nThen, when ever the OnCanExecuteChnaged is called, we use the SyncContext if we are on a different thread to post the event to the UI thread.\r\n\r\n```\r\n        protected virtual void OnCanExecuteChanged()\r\n        {\r\n            if (_synchronizationContext != null)\r\n                _synchronizationContext.Post((o) => CanExecuteChanged?.Invoke(this, EventArgs.Empty), null);\r\n            else\r\n                CanExecuteChanged?.Invoke(this, EventArgs.Empty);\r\n        }\r\n```\r\n\r\nThe issue comes when I write a simple test like this:\r\n```\r\n        [Fact]\r\n        public void RaiseCanExecuteChangedRaisesCanExecuteChanged()\r\n        {\r\n            var handlers = new DelegateHandlers();\r\n            var command = new DelegateCommand<object>(handlers.Execute);\r\n            bool canExecuteChangedRaised = false;\r\n            command.CanExecuteChanged += delegate { canExecuteChangedRaised = true; };\r\n\r\n            // we called our OnCanExecuteChanged method\r\n            command.RaiseCanExecuteChanged(); \r\n\r\n            // SyncContext.Post hasn't completed so this fails\r\n            Assert.True(canExecuteChangedRaised);\r\n        }\r\n```\r\n\r\nHow on earth do I tell my xUnit test to wait until th epost has competed, or turn off the SyncContext all together per test?"}}
{"actor":{"display_login":"brianlagunas"}}