No connection could be made because the target machine actively refused it. (localhost:7128)

93 Views Asked by At

I created test case for my scenario. while I am debugging the test case, I am getting No connection made error. I looked for many solution .But still issue is not resolved. Port Number which is mentioned inside base address is correct and also I disabled the fire wall too. But still issue exists. I created the scenario in dot net 7 and wrote the Nunit Test case.

ERROR No connection could be made because the target machine actively refused it. (localhost:7128) ----> System.Net.Sockets.SocketException : No connection could be made because the target machine actively refused it.

FizzBuzz Project

Controller

 using FizzBuzz.IServices;
 using Microsoft.AspNetCore.Mvc;
 using FizzBuzz.Models;
 using FizzBuzz.Services;

 namespace FizzBuzz.Controllers
 {
   public class FizzBuzzController : Controller
   {
    public IActionResult Index()
    {
        return View();
    }

    // Controllers/FizzBuzzController.cs
    [HttpPost]
    public IActionResult Index( string inputValues)
    {
        ViewBag.InputValues = inputValues;
        List<FizzBuzzModel> results = new List<FizzBuzzModel>();

            // Split the input values into an array
            var values = inputValues.Split(',');

            foreach (var value in values)
            {
                var result = SingletonFizzBuzzService.Instance.ProcessInput(value.Trim());
                results.Add(result);
            }
        
        return View(results);
    }
  }
}

FizzBuzzTest Project

FizzBuzzTest class

    [TestFixture]
    public class FizzBuzzTest
    {       
        private HttpClient _client;
        [SetUp]
        public void SetUp()
        {
            _client = new HttpClient();
            _client.BaseAddress = new Uri("https://localhost:7128/");
        }

        [TearDown]
        public void TearDown()
        {
            _client.Dispose();
        }

        [TestCase("1", "Divided 1 by 3 and Divided 1 by 5")]
        [TestCase("3", "Fizz")]
        [TestCase("5", "Buzz")]
        [TestCase("15", "FizzBuzz")]
        public async Task Index_ReturnsCorrectResult(string input, string expectedResult)
        {
            // Arrange
            var content = new StringContent($"inputValues={input}", Encoding.UTF8, "application/x-www-form-urlencoded");
            //var response = await _client.PostAsync("https://localhost:7128/FizzBuzz/Index", content);
            var response = await _client.PostAsync("/FizzBuzz/Index", content);
            response.EnsureSuccessStatusCode();
            var responseContent = await response.Content.ReadAsStringAsync();

            // Assert
            Assert.IsNotNull(responseContent, "Content should not be null");
            Assert.IsTrue(responseContent.Contains(expectedResult), $"Expected result '{expectedResult}' not found in the content: {responseContent}");
        }
    }
}

As per below mention line it should go to FizzBuzz controller and hit the action. But it is not going to the controller.

  var response = await _client.PostAsync("/FizzBuzz/Index", content); 
0

There are 0 best solutions below