Is it possible to create mock api on this cypress to prevent the actual api call - vue.js

We have an vue js application which contains the component, On this component, there is an api call to update database records. We are doing unit test for this component.
Is it possible to create mock api on this cypress to prevent the actual api call? So database will not be modified during the unit test and can keep actual data as it is on database.

You can mock it indeed. For example this mocks a server and a specific API-call:
cy.server()
cy.route('POST', '**/oauth/v2/token', 'fixture:/oauth/agent-token.json')
More information about route is available on the cypress site: https://docs.cypress.io/api/commands/route.html

To make sure every request is mocked, use the force404 option with cy.server:
cy.server({force404: true})
cy.route('**/user/jake', {user:{name:'Jake'})
cy.visit('/')
// your test code here
Then any XHR request to /user/jake will work, but /user/jane and /login for example, will 404

Related

Why not mock multiple axios call on same page using Jest in React Native?

I am making two or more requests on same page, and it would be difficult to manage that with mock return values -- as well as brittle: if you re-arrange your page then your tests could fail because your return values were in the wrong order on your mock.
So, I am checking for any other way to mock return values in jest.
In React.js MSW tool is available to handle the above scenario but in React Native I didn't find such tool to handle above scenario
Any Suggestions?
If your calling multiple axios.get Api calls on single page, then to avoid conflict you have to create separate function for each Api
e.g.
1.first Api call
const firstCall= jest.fn();
axios.get = firstCall.mockResolvedValueOnce(dummy_data).
2.second Api call
const secondCall= jest.fn();
axios.get = secondCall.mockResolvedValueOnce(dummy_data2).
If you do this, your test cases will work as expected.

How to pass query params of a request url while mocking with Request Hooks in Testcafe?

My request url has multiple query params. I want to mock the response of the external API while writing my tests in Testcafe. I want to mock the data in E2E testing because my data will expire in every 15days.
Please suggest some ways to handle mocking the request with query param. I am able to mock the request not having any query param and I am using Request Hooks for that.
There shouldn't be a difference between a mocking response from URL with parameters and without them. You can mock a response by using RequestMock API:
...
const getDataMock = RequestMock()
.onRequestTo(/.*getData\?param=param_1/)
.respond((req, res) => {
res.setBody({...});
});
...
If this doesn't work for you, please provide us with the code where you define RequestMock or RequestHook.

In TestCafe Is possibile to register xhr and use as mocking (automocking)

I'm using testCafe for my functional test.
My project used a lot of XHR request and I don't want to waste my time to generate each single mock.
Exists an automocker like this: https://github.com/scottschafer/cypressautomocker for testcafe?
TestCafe does not provide the described functionality out of the box. However, you can use the combination of RequestLogger and RequestMock
The idea is in that you can create a JSON file with request results at the first run using the RequestLogger.
Then, based on results of the first run, you can configure your RequestMock object to respond with the results from the file for all consequent requests.

Create mocks in api functional testing with Symfony

I'm dealing with a problem I have in a Symfony 4 API functional tests. My functional tests consists in making requests to the API and analyze the response given. I've been working like this and works fine.
The problem comes with a new API method I'm implementing which needs the perform a request to an external service. I want to mock during my tests, but I don't know how can I create a mock that persists when the API receives the request from the functional test.
I've been thinking about something like create mocks which are always used in the test environment but I haven't found anything...
you can check in http-client service called url and if it compare your external api url return certain response, it will be look something like this:
$guzzleServiceMock = $this
->getMockBuilder(GuzzleHttp\Client::class)->disableOriginalConstructor()
->setMethods(['get'])
->getMock();
$guzzleServiceMock
->expects($this->any())
->method('get')
->with(
$this->stringContains('/external/api/route')
)
->willReturnCallback(
function ($uri, $options = []) {
return new Response(
200,
[],
'{"result": {
"status": "success",
"data": "fake data",
}}'
);
}
);
next step you will need to inject service into container, with this question you can look this repo, there are good examples of how this can be done: https://github.com/peakle/symfony-4-service-mock-examples/blob/master/tests/Util/BaseServiceTest.php
I'm not sure if I understand correctly, but if you'd like to mock an external API (meaning your application is connecting to another application on another server), one solution would be to actually lunch a mock server to replace your actual external server.
Either you implement such a mock server yourself or you use an existing solution such as http://wiremock.org/

Securing API keys in Nuxtjs

I've researched and found three different possibilities to solving my case: I'd like to make an async API call (using dotenv variables to store the credentials) and commit the returned data to Vuex on app init --keeping the creds secure.
Currently I'm attempting using serverMiddleware, but I'm having trouble accessing the context. Is this possible? Currently just getting a "store is not defined" error.
Also, after researching, I keep seeing that it's not a good idea to use regular middleware, as running any code on the client-side exposes the env variable... But I'm confused. Doesn't if (!process.client) { ... } take care of this? Or am I missing the bigger picture.
Additionally, if it does turn out to be okay to use middleware to secure the credentials, would using the separate-env-module be wise to make doubly sure that nothing gets leaked client-side?
Thanks, I'm looking forward to understanding this more thoroughly.
You can use serverMiddleware.
You can do it like this:
client -> call serverMiddleware -> servermiddleware calls API.
that way API key is not in client but remains on the server.
Example:
remote api is: https://maps.google.com/api/something
your api: https://awesome.herokuapp.com
since your own api has access to environment variables and you don't want the api key to be included in the generated client-side build, you create a serverMiddleware that will proxy the request for you.
So that in the end, your client will just make a call to https://awesome.herokuapp.com/api/maps, but that endpoint will just call https://maps.google.com/api/something?apikey=123456 and return the response back to you