1

I have the following code with gives the correct assert result but the test case errors with the message "Object synchronization method was called from an unsynchronized block of code." What can I do to correct the issue?

        public async Task Test()
    {
        using (new FakeHttpContext.FakeHttpContext())
        {
            BasketController _controller = new BasketController();
            List<StockMovementModel> model = new List<StockMovementModel>
        {
            StockMovementFixtures.TestFixture()
        };
            var result = await _controller.CreateTransfer(model, new Kendo.Mvc.UI.DataSourceRequest()).ConfigureAwait(false);
            Assert.IsNotNull(result);
        }

    }
AdamG31
  • 23
  • 5
  • You want to "mock", not "fake". See here https://stackoverflow.com/questions/4379450/mock-httpcontext-current-in-test-init-method or https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/november/async-programming-unit-testing-asynchronous-code for how to unit test `async` code – eglease Sep 16 '21 at 14:28
  • @eglease if I take out the faking I have errors with the code to locate the directory ( string workingDirectory = HostingEnvironment.MapPath) – AdamG31 Sep 16 '21 at 14:31
  • I just meant that the correct term for fake classes used for testing is "mocking". You can still call the actual object "fake" but what you are trying to do is "mock" `HttpContext`. – eglease Sep 16 '21 at 14:41
  • You may want to investigate Mocking "HttpMessageHandler" instead of HttpClient. breadcrumb code : Mock handlerMock = new Mock(); HttpClient magicHttpClient = new HttpClient(handlerMock.Object); – granadaCoder Aug 29 '22 at 13:10

1 Answers1

0

Workarround that seems to be working :

using (var ctx = new FakeHttpContext()) { lock (ctx) { ...... } }
cyvocross
  • 21
  • 2