mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Consistent API for defining expectations. #292
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @cezarypiatekGC on GitHub (Sep 11, 2020).
Hi,
I would like to use WireMock.Net in my project but I need to handle the following scenario. When we run tests locally (in VisualStudio) then we want to run WireMock in memory using WireMockServer.Start. When we run tests in CI then we want to use docker container with WireMock and connect to it from the test code and from the tested application. Besides that difference, we would like to have a consistent API to define mocking rules. However, it doesn't seem to be possible right now because WireMock client uses different models to define mappings (MappingModel) and the MappingConverter which could convert IMapping to MappingModel has an internal modifier. Is it possible to expose the method which could perform that conversion? Or am I doing something wrong and there's an option to define Mappings once and use it with WireMockServer and with standalone (docker) app? Ofc, WireMockServer has WithMapping method which accepts MappingModel but creating MappingModel manually seems to be a very tedious task. I Would prefer to use that fluent interface with Request.Create() and Response.Create()
@StefH commented on GitHub (Sep 11, 2020):
Hello @cezarypiatekGC,
For your scenario I would recommend to use (static) mapping files to be used in your local unit-tests and by your WireMock.net running in a docker container.
1. Create a mapping.json file
For the format, see https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#post---__adminmappings
And place this file in the __admin/mappings folder.
2. Now in your unit tests, you can use:
The unit tests will use the mappings defined in the mapping.json file.
See also https://github.com/WireMock-Net/WireMock.Net/wiki/Settings#readstaticmappings
3. Docker
Getting this to work in docker container is not 100% easy supported because loading that mapping.json file can not be done at startup because the docker image is already created.
Some options what you could do is after the docker container has been started, just use the admin rest interface to post the mapping.json content.
You can just use any tool to do a http post to the /__admin endpoint, or use the c# client api:
https://github.com/WireMock-Net/WireMock.Net/wiki/Admin-API-Reference#client-api
@StefH commented on GitHub (Sep 11, 2020):
Or, you can also just run a console app instead of a docker image. See https://github.com/HeadsInsane/Challenge/tree/master/src/BankSimulator and the related main project on how it's also possible.
@cezarypiatekGC commented on GitHub (Sep 11, 2020):
Hi @StefH
Thanks for your response. Is there any reason that prevents you from exposing
MappingConverter? That would simply a lot.@StefH commented on GitHub (Sep 11, 2020):
There is no real reason, except that I'd like to expose only the classes which are really used by the users.
But if it helps you, I can make this class public. And add a comment like
This class is used by WireMock.Net internally. It isn't intended for use in application code.@StefH commented on GitHub (Oct 1, 2020):
@cezarypiatekGC What do you think of my previous answer?
@cezarypiatekGC commented on GitHub (Oct 6, 2020):
I started using
WithMappingmethod to define mocks (and created my own abstraction over this tedious API), but there are some discrepancies in the final behavior of those two API's, the problem is reported here https://github.com/WireMock-Net/WireMock.Net/issues/504@StefH commented on GitHub (Oct 6, 2020):
Hello @cezarypiatekGC,
If you want to test a preview fix for this issue, in order to test that you need MyGet version
WireMock.Net.1.2.18-ci-13754.nupkg.Can you test if that one does work correct?
@StefH commented on GitHub (Mar 3, 2022):
@cezarypiatekGC
I close this issue.
In case you still have questions, just create a new issue.