-1

We are working on a system that uses a asp.net rest-api as an backend. This backend is connected locally with a machine that does some things after the backend gets called. The backend has its own state e.g. a call on route 1 changes the response of route 2.

We want to test the frontend without the connected machine but we are having a time to to figure out what the best way would be.

I guess there are several options:

  1. Mock the machine and run the whole backend to test it with the frontend.
  2. Mock the API with a self coded fake api to include the logic in the backend
  3. Use a fake/mock api tool

We think that we cant do solution 1 because the communitcion to the machine is quite complex. Solution 2 would include some heavy coding. We need to copy all the routes and implement fake logic. Solution 3 would be the easiest, but we didn't find any good tool that can work with a global state.

Any suggestions?

kingrazer
  • 58
  • 1
  • 5
  • 1
    Have you tried [WireMock.Net](https://github.com/WireMock-Net/WireMock.Net)? It does support [scenarios](https://github.com/WireMock-Net/WireMock.Net/wiki/Scenarios-and-States). – Peter Csala Jun 01 '22 at 07:56
  • 1
    Hint: Questions with a title that starts with _"Best way to ..."_ are mostly pretty quickly closed as opinion-based. You may want to change that phrasing. I personally would change it to simply "How to ..." – Fildor Jun 01 '22 at 07:58
  • How is your backend hosted? Is it a "Cloud-Service"? If so, I guess the way to go is to have a "Test"-Environment set up exactly for that purpose. – Fildor Jun 01 '22 at 08:02
  • @kingrazer Here are two examples how you can use it: [1](https://stackoverflow.com/a/69207963/13268855), [2](https://stackoverflow.com/questions/68532973/unit-test-polly-check-whether-the-retry-policy-is-triggered-on-timeout-error/68540520#68540520) – Peter Csala Jun 01 '22 at 08:20

1 Answers1

0

On the client side part I would use and end to end testing framework like selenium or karma+something for the frontend if you need to fake browser usage too. There are headless browsers too, which can make things faster. https://en.wikipedia.org/wiki/Headless_browser Otherwise a few HTTP calls to the frontend and checking the response can be ok too.

As of mocking out the REST service, I would just create a mock object that does the same as the REST client object (not sure what you have with .net) with a few hardcoded responses and inject it to the frontend. So there would be no actual HTTP calls. If it is not injectable, then the only solution is making a fake REST API or use the actual one with a testing database.

I am not sure if there are existing .net testing frameworks which support all of this. The most basic solution would be having a central testing log of frontend browser errors, frontend HTTP requests and responses, (mock) REST API HTTP requests and responses and check on this log if the expectations are met for the behavior of the frontend. This can be done with Selenium tests and a proxy that logs HTTP requests and responses and somehow delivers it to the Selenium tests.

inf3rno
  • 24,976
  • 11
  • 115
  • 197