-
-
Notifications
You must be signed in to change notification settings - Fork 214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WireMock not working in Debug within test project #510
Comments
Please provide a full working example. |
Thank you for the fast response, here is a full working example: using NUnit.Framework;
using System.Threading;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
namespace NUnitTestProject1
{
public class Tests
{
WireMockServer server;
[SetUp]
public void Setup()
{
//choosing a port so we know which port to hit when testing this.
server = WireMockServer.Start(port: 50100);
}
[Test]
public void Test_With_Breakpoint()
{
server.Given(
Request.Create().WithPath("/test"))
.RespondWith(Response.Create().WithBody("Breakpoint Test"));
} // Breakpoint here
[Test]
public void Test_Without_Breakpoint()
{
server.Given(
Request.Create().WithPath("/test"))
.RespondWith(Response.Create().WithBody("Sleep Test"));
Thread.Sleep(60000);
}
}
} |
@xadvfh
My own development environment has some issues now, so I cannot reproduce your issue right now. I'll come back to you. |
I'll try with xunit later today and see if that helps. |
@xadvfh In the |
@StefH how would that work in running tests in parallel. Resetting would delete all mappings for other tests currently running, no? |
I'm not 100% sure for NUnit, but I think that all tests within a single .cs file are run sequentially. |
So I just tested with the latest version of xunit and it has the same problem. I even removed any setup related stuff and am creating the server in the test itself. Working example: using System.Threading;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
using Xunit;
namespace XUnitTestProject
{
public class Tests
{
[Fact]
public void Test_With_Breakpoint()
{
var server = WireMockServer.Start(port: 50100);
server.Given(
Request.Create().WithPath("/test"))
.RespondWith(Response.Create().WithBody("Breakpoint Test"));
} // Breakpoint here
[Fact]
public void Test_Without_Breakpoint()
{
var server = WireMockServer.Start(port: 50100);
server.Given(
Request.Create().WithPath("/test"))
.RespondWith(Response.Create().WithBody("Sleep Test"));
Thread.Sleep(60000);
}
}
} |
My NUnit test class: using System.Threading;
using NUnit.Framework;
using WireMock.RequestBuilders;
using WireMock.ResponseBuilders;
using WireMock.Server;
namespace NUnitTestProject1
{
public class Tests
{
WireMockServer server;
[OneTimeSetUp]
public void Setup()
{
server = WireMockServer.Start();
}
[Test]
public void Test_With_Breakpoint()
{
int x = 0;
server.Reset();
server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test"));
int y = 0;
} // Breakpoint here
[Test]
public void Test_Without_Breakpoint()
{
int x = 0;
server.Reset();
server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Sleep Test"));
Thread.Sleep(1000);
int y = 0;
}
}
} |
Were you able to make a request from a browser/postman while stopped at a breakpoint after the stubbing(line 24)? |
No, this is not possible. UnitTesting with WireMock needs to be done completely in the unit-test like this: [Test]
public async Task Test_With_Breakpoint()
{
int x = 0;
server.Reset();
server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test"));
var client = new HttpClient();
var response = await client.GetAsync(server.Urls.First() + "/test");
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
var content = await response.Content.ReadAsStringAsync();
Assert.AreEqual("Breakpoint Test", content);
int y = 0;
} // Breakpoint here |
I understand. My usecase is that we're creating complex json object and during the creation phase it would be nice to be able to see what we're creating in a more readable manner. Is there a technical limitation why setting the breakpoint doesn't work but adding a long sleep works fine? |
The debugger probably stops several threads, so also the code which listens and responses to requests. About complex json, is this the response message? |
Is there a way you recommend setting up the wiremock server so we can do debugging? for example, if you create an asp.net core API you're able to debug.
Yes. we're mocking an API that is used by a UI. We have models set up but there are nested objects. On top of that, we have created builders to allow test writers to create responses dynamically based on what they need. |
If you just want to verify if the JSON is correct, then the easiest solution is to created your complex nested objects and serialize these to a json-stirng yourself. And then provide this json-string to the code: |
do you have an idea of where in the codebase this issue could be happening? maybe I can do some digging and see if we can fix this. |
@StefH just checking in to see if there is anything I can help with to fix this. Is there a place in the code you would recommend to start troubleshooting? |
Maybe change the async behavior from the GetAsync? [Test]
public async Task Test_With_Breakpoint()
{
int x = 0;
server.Reset();
server.Given(Request.Create().WithPath("/test")).RespondWith(Response.Create().WithBody("Breakpoint Test"));
var client = new HttpClient();
var responseTask = client.GetAsync(server.Urls.First() + "/test");
// put breakpoint here + debug?
var content = await (await responseTask).Content.ReadAsStringAsync();
Assert.AreEqual("Breakpoint Test", content);
} // Breakpoint here |
@xadvfh Do you still have an issue? Or can this one be closed? |
@StefH I still have this issue. |
@StefH I tried this again yesterday to wanted to confirm this issue still exists. My use case is that I am using WireMock.Net to replace API calls that are made by our website. At times i want to place a breakpoint to pause the test and click around in the website. However, anything that triggers a call to wiremock will not respond while i am in a breakpoint. |
Hello @xadvfh, Did you try the latest version yet? |
@xadvfh : can you please check the latest version ? |
Describe the bug
When you place a breakpoint and try to make a call to the mock server via an external tool (browser), there is no response. This was first seen in #122
It would be nice if this worked since you could easily check if the json response bodies you're creating are working as expected. Right now I have to add a long
Thread.Sleep()
to check my work.Expected behavior:
Server should be listening in debug mode.
Test to reproduce
Other related info
I'm using dotnet core 3.1 and nunit 3.
The text was updated successfully, but these errors were encountered: