unit-testing

Pytest - How to Parameterize tests with multiple scenarios?

倾然丶 夕夏残阳落幕 提交于 2021-01-29 17:55:28
问题 I'm using pytest ,boto3 and aws and want to have dynamic assertions with parameterized tests. How to improve this code to only assert on a specific group of subnetids? production_private_ids = ["subnet-08f6d70b65b5cxx38", "subnet-0b6aaaf1ce207xx03", "subnet-0e54fda8f811fxxd8"]) .... nonproduction_private_ids = ["subnet-11f6xx0b65b5cxx38", "subnet-116aaaf1ce207xx99", "subnet-11xxfda8f811fxx77"]) .... @pytest.mark.parametrize("subnet", ["production_private_ids", "nonproduction_private_ids",

Mocking gRPC status code ('RpcError' object has no attribute 'code') in Flask App

北慕城南 提交于 2021-01-29 14:51:50
问题 I have to create a unittest that should mock a specific grpc status code (in my case I need NOT_FOUND status). This is what i want to mock: try: # my mocked function except grpc.RpcError as e: if e.code() == grpc.StatusCode.NOT_FOUND: # do something My unittest until now looks like this: def mock_function_which_raise_RpcError(): e = grpc.RpcError(grpc.StatusCode.NOT_FOUND) raise e class MyTestCase(BaseViewTestCase): @property def base_url(self): return '/myurl' @mock.patch('my_func', mock

Why doesn't jest.spyOn() sometimes work on a Vue component's method?

独自空忆成欢 提交于 2021-01-29 14:18:02
问题 I see the changes the method makes as event handler, but jest.spyOn(wrapper.vm, 'methodName') doesn't catch the call in major cases, but in some tests it somehow works. I suspect a possible re-render (maybe, due to options argument in mount() call), that replaces the original method with the jest.spyOn() , because, if I trigger event twice in a row, toHaveBeenCalled() fulfills and tracks the call. The component: <template> <v-app-bar app color="primary" dark class="header" > <router-link :to=

Can JMockit mock constructors with any argument?

淺唱寂寞╮ 提交于 2021-01-29 13:46:03
问题 I am replacing PowerMock with JMockit in old unit testing case. Below is the PowerMock sample code which mock the File.class constructors with any argument . Can JMockit mock constructors with any argument like it ? The situation is this:myFile is a mock. And I want to simulate returning myFile when calling any constructor in the File class..So What is the code like. // PowerMock whenNew(File.class).withAnyArguments().thenReturn(myfile); // JMockit new Expectations() {{ new File(anyString);

Jest mock not resolving to each injected value

☆樱花仙子☆ 提交于 2021-01-29 13:39:51
问题 I'm trying to test a Firebase cloud function named myCloudFn in my functions/send.js module. My tests are in functions/test/send.spec.js : // send.js const admin = require('firebase-admin'); async function myCloudFn (email) { const authUser = await admin.auth().getUserByEmail(email); return authUser; } module.exports = { myCloudFn }; // send.spec.js const send = require('../send.js'); jest.mock('firebase-admin', () => ({ auth: () => ({ getUserByEmail: jest.fn() .mockResolvedValueOnce({ uid:

How to test the existence of a route in Angular using Jasmine?

六眼飞鱼酱① 提交于 2021-01-29 11:27:58
问题 I'm trying to write a test that checks for the existence of a route using Jasmine for Angular application: import { routes } from './app-routing.module'; import { UsersComponent } from './users/users.component'; describe('routes', () => { it('should contain route for /users', () => { const expectedRoute = { path: 'users', component: UsersComponent }; expect(routes).toContain(expectedRoute); }); }); but the test fails (probably because of object equality in Javascript How object equality

How to mock HTTP Request in Angular?

时间秒杀一切 提交于 2021-01-29 10:39:17
问题 I have checked a lot of articles and answers but I don't seem to find the right way to mock HTTP Requests for my methods. I want to test my frontend application independently from the backend . Here is the type of methods I have: private getProfile() { this.http .get('go/profile/get', {withCredentials: true}) .subscribe((profile: Profile) => { this.user.profile = profile; this.updateLineMsgs(); }); } Any suggestions ? 回答1: Usually i mock my Http requests with HttpClientTestingModule : import

How to mock the new HttpClientFactory in .NET Core 2.1 using Moq

梦想的初衷 提交于 2021-01-29 10:00:44
问题 .NET Core 2.1 comes with this new factory called HttpClientFactory , but I can't figure out how to mock it to unit test some methods that include REST service calls. The factory is being injected using .NET Core IoC container, and what the method does is create a new client from the factory: var client = _httpClientFactory.CreateClient(); And then using the client to get data from a REST service: var result = await client.GetStringAsync(url); 回答1: The HttpClientFactory is derived from

How to test simple boolean functions?

ぐ巨炮叔叔 提交于 2021-01-29 09:15:53
问题 Im having some issues to understand testing, since I almost never find it neccessary. If I have simple functions like function isMovementOutOfBounds(newPosition) { if (newPosition[0] > input[0][0] || newPosition[0] < 0 || newPosition[1] > input[0][1] || newPosition[1] < 0) { return true; } return false; } or function isMovementForbidden(forbiddenMovements, initialPosition, newPosition) { function isArrayInArray(arr, item) { const item_as_string = JSON.stringify(item); const contains = arr

How to test simple boolean functions?

偶尔善良 提交于 2021-01-29 09:06:39
问题 Im having some issues to understand testing, since I almost never find it neccessary. If I have simple functions like function isMovementOutOfBounds(newPosition) { if (newPosition[0] > input[0][0] || newPosition[0] < 0 || newPosition[1] > input[0][1] || newPosition[1] < 0) { return true; } return false; } or function isMovementForbidden(forbiddenMovements, initialPosition, newPosition) { function isArrayInArray(arr, item) { const item_as_string = JSON.stringify(item); const contains = arr