在我的dotnet-testcontainers测试中,我使用dotnet-testcontainers https://github.com/HofmeisterAn/dotnet-testcontainers来旋转一个带有xUnit的容器。我可以成功地创建一个挂载银行客户端,并成功地创建一个冒名顶替者。问题是,当测试运行时,应用程序试图调用http://localhost:3000上的冒名顶替者,却拒绝了连接。
我可以成功地打开http://localhost:2525,并可以看到Mountebank默认页面。所以坐骑银行运转良好。我还确认,通过查看码头容器日志,在端口3000上成功地创建了这个冒充者。我还试着用邮递员给我在http:localhost:3000的冒名顶替者打电话,结果被拒绝了。
有什么问题吗?这是否是码头容器中的端口3000没有公开之类的问题?下面是我的代码:
MountebankClient mbClient = new MountebankClient();
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("bbyars/mountebank")
.WithName("mountebank")
.WithPortBinding(2525, false)
.WithHostname("localhost");
var testContainers = testcontainersBuilder.Build();
await testContainers.StartAsync();
var testImposter = mbClient.CreateHttpImposter(3000);
testImposter.AddStub().ReturnsBody(HttpStatusCode.OK, File.ReadAllText(@".\Stubs\testImposter.json"));
mbClient.Submit(testImposter);发布于 2021-12-02 05:38:11
可能对其他会面临这个问题的人有用。搞清楚问题出在哪里了。我没有映射容器中的主机端口和冒牌货端口。了解已发布的端口https://docs.docker.com/config/containers/container-networking/和使用WithExposedPort(3000),然后使用WithPortBinding(3000,3000)对该端口进行端口绑定,其中,该方法中的first端口是主机端口,second端口是容器端口。
var testcontainersBuilder = new TestcontainersBuilder<TestcontainersContainer>()
.WithImage("bbyars/mountebank")
.WithName("mountebank")
.WithPortBinding(2525, 2525)
.WithExposedPort(3000)
.WithExposedPort(3100)
.WithPortBinding(3000, 3000)
.WithPortBinding(3100, 3100)
.WithWaitStrategy(Wait.ForUnixContainer().UntilPortIsAvailable(2525))
.WithHostname("localhost");https://stackoverflow.com/questions/70165557
复制相似问题