小编Laz*_*ass的帖子

spring webflux 如何在 webfilter 中获取请求正文?

我想在 webfilter 中获取请求正文。

现在,当请求体很大时,

final Flux<DataBuffer> body = exchange.getRequest().getBody();
Run Code Online (Sandbox Code Playgroud)

这个身体有555号,

如何在 webfilter 中获取请求正文?

spring-boot reactor-netty spring-webflux

5
推荐指数
0
解决办法
1531
查看次数

Jest onSpy - 预期已调用模拟函数

我正在努力使用间谍On作为测试我的utils.js模块的一部分。我尝试了各种方法和途径,但似乎都产生了“预期的模拟函数被调用”。根据记录,其他单元测试工作正常,因此我的实际测试设置不应该有任何问题。

下面是一个简化的测试用例,包含两个函数和一个测试,我什至无法让它们工作。我是否完全误解了间谍?

// utils.js
function capitalHelper(string){
  return string.toUpperCase();
}

function getCapitalName(inputString){
  return capitalHelper(inputString.charAt(0)) + inputString.slice(1);
}

exports.capitalHelper = capitalHelper
exports.getCapitalName = getCapitalName



// utils.test.js
const Utils = require('./utils');

test('helper function was called', () => {
  const capitalHelperSpy = jest.spyOn(Utils, 'capitalHelper');
  const newString = Utils.getCapitalName('john');
  expect(Utils.capitalHelper).toHaveBeenCalled();
})
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing mocking spy jestjs

5
推荐指数
1
解决办法
2万
查看次数

Joda-Time new DateTime() vs DateTime.now()

从 Joda-Time time version 2.0org.joda.time.DateTime#now()开始,引入了静态方法。对我来说,不清楚使用new DateTime()(因为代码只是委托)有什么好处。

public static DateTime now() {
    return new DateTime();
}
Run Code Online (Sandbox Code Playgroud)

同样从 java doc 中,我不清楚我应该更喜欢哪一个。

new DateTime
Run Code Online (Sandbox Code Playgroud)

使用ISOChronology默认时区获取设置为当前系统毫秒时间的 {@code DateTime} 。

DateTime#now()
Run Code Online (Sandbox Code Playgroud)

使用ISOChronology默认时区构造一个设置为当前系统毫秒时间的实例。

有人可以解释在哪个用例中应该首选哪个用例吗?

java jodatime

5
推荐指数
1
解决办法
2545
查看次数

如何使用Vue Test utils测试输入是否集中?

我有一个专注于输入的指令。我想测试该指令。唯一的问题是我找不到如何测试输入是否聚焦的问题

这是我简单的模拟组件

 <template>
  <div v-directive>
    <input/>
  </div>
</template>
Run Code Online (Sandbox Code Playgroud)

这是我的指令:

import Vue from 'vue'

export const focusInput = () => {
  return {
    inserted(el: any) {
      el.getElementsByTagName('input')[0].focus()
    }
  }
}

export default Vue.directive('focus-input', focusInput())
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

import Vue from 'vue'
import { mount , createLocalVue} from '@vue/test-utils'

import FocusInputMock from './mockComponents/FocusInputMock.vue'
import {focusInput} from '@/directives/focusInput';

const localVue = createLocalVue()

localVue.directive('directive',  focusInput())


test('update content after changing state', () => {
  const wrapper = mount(FocusInputMock, {
    localVue
  })
  Vue.nextTick(function () {
    expect(wrapper.find('input')) // I …
Run Code Online (Sandbox Code Playgroud)

vue.js jestjs vue-test-utils

5
推荐指数
1
解决办法
1622
查看次数

使用 Jest 测试 Firebase RealtimeDatabase

我的目的只是测试一个功能。我无法弄清楚如何正确模拟 firebase。我尝试使用 Jest 文档中的 axios 模拟来保留示例。我有以下代码:

音乐服务.js

import { initializeApp } from "firebase/app";
import "firebase/database";

const firebase = initializeApp({
  apiKey: "<API_KEY>",
  authDomain: "<PROJECT_ID>.firebaseapp.com",
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
  projectId: "<PROJECT_ID>",
  storageBucket: "<BUCKET>.appspot.com",
  messagingSenderId: "<SENDER_ID>",
});

export class MusicService {
  static getAlbums() {
    return firebase.database().ref("albums").once("value")
    .then(snapshot => Object.values(snapshot.val()));
  }
}
Run Code Online (Sandbox Code Playgroud)

音乐服务.test.js

import firebase from 'firebase/app';
import 'firebase/database';
import { MusicService } from './MusicService';

jest.mock('firebase/app');
jest.mock('firebase/database');

test("test", () => {
  firebase.initializeApp.mockImplementation(() => {
    database: jest.fn(() => {
      return {
        ref: jest.fn()
      }
    })
  });

  MusicService.getAlbums(); …
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing firebase jestjs firebase-realtime-database

5
推荐指数
1
解决办法
3323
查看次数

三元运算符测试用例开玩笑

我想为以下案例编写一个开玩笑的测试用例,因为它显示了分支覆盖率50%并指出了这段代码。

render() {
        const {
          isExit
        } = data;
        const text = isExit ? 'Yes' : 'No';
Run Code Online (Sandbox Code Playgroud)

或者

<LabelValue label="New Line" value={isExit ? 'Yes' : 'No'} />
Run Code Online (Sandbox Code Playgroud)

测试用例

it('Should display the data if API status is complete', () => {
    const wrapper = shallowWithTheme(<DataPage
      orderDetail={{ isExit: true}}
      theme={theme}
    />);

    // what to write here?   
  });
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs enzyme

5
推荐指数
1
解决办法
3556
查看次数

开玩笑地嘲笑温斯顿记录器

我在想如何模拟运输。文件(来自温斯顿节点模块)。我正在使用 jest,所以__mocks__/winston.ts是自动加载的。我认为我不能嘲笑它,因为有new

// LoggerFactory.ts
import { transports, TransportInstance } from "winston";

...
    const transportList: TransportInstance[] = [
        new transports.File({
            name: `${tag}-error`,
            filename: `${dirname}${filename}.error.log`,
            json: false,
            level: "error",
            zippedArchive: false,
            maxFiles: 14,
            maxsize: 100000000
        }),
        new transports.File({
            name: `${tag}-info`,
            filename: `${dirname}${filename}.log`,
            json: false,
            level: "info",
            maxFiles: 10,
            zippedArchive: false,
            maxsize: 100000000
        })
    ];
...

// __mocks__/winston.ts
const winston = {
    ????
};
export default winston;
Run Code Online (Sandbox Code Playgroud)

错误:类型错误:无法读取未定义的属性“文件”

javascript mocking winston typescript jestjs

5
推荐指数
1
解决办法
5703
查看次数

用Jest-Enzyme测试样式化的组件

如何测试我styled-component是否具有特定的CSS属性值对?

假设我的组件如下:

const myColor = '#111';

const Button = styled.button`
  background-color: ${myColor};
`;
Run Code Online (Sandbox Code Playgroud)

然后测试检查该'background-color'属性是否具有值'#111'

测试运行者是Jest,我使用测试实用程序库Enzyme

unit-testing reactjs jestjs enzyme styled-components

5
推荐指数
1
解决办法
2058
查看次数

单元测试控制器使用 Jest、NodeJS

我想检查某些路由调用正确控制器使用Jest specific (mock or spy) 的情况

它是单元测试特定的案例。有人可以帮助我如何检查它使用笑话。我不需要验证类型的期望(状态代码或 res 对象)我需要检查控制器是否已被调用。谢谢!

例如:

// todoController.js
function todoController (req, res) {
    res.send('Hello i am todo controller')
} 

// index.spec.js
const express = require('express');
const request = require('request-promise');
const todoController = require('./todoController');
jest.mock('./todoController');

const app = express();

app.get('/todo', todoController)

test('If certain routes are calling the correct controller , controller should to have been called times one.', async() => {
    await request({url: 'http://127.0.0.1/todo'})
    expect(todoController).toHaveBeenCalledTimes(1);
})
Run Code Online (Sandbox Code Playgroud)

javascript unit-testing node.js express jestjs

5
推荐指数
1
解决办法
8637
查看次数

如何在开玩笑中模拟window.alert方法?

我有以下React组件:

class Form extends React.Component {

   constructor(props) {
      super(props);
      this.state = this._createEmptyTodo();
   }

   render() {

      this.i18n = this.context;

      return (
         <div className="form">
            <form onSubmit={this._handleSubmit.bind(this)}>

               <input
                  placeholder={this.i18n.placeholders.addTitle}
                  type="text"
                  value={this.state.title}
                  onChange={this._handleTitleChange.bind(this)}></input>

               <textarea
                  placeholder={this.i18n.placeholders.addDescription}
                  value={this.state.description}
                  onChange={this._handleDescriptionChange.bind(this)}></textarea>

               <button>{this.i18n.buttons.submit}</button>
            </form>
         </div>
      );
   }

   _handleTitleChange(e) {
      this.setState({
         title: e.target.value
      });
   }

   _handleDescriptionChange(e) {
      this.setState({
         description: e.target.value
      });
   }

   _handleSubmit(e) {

      e.preventDefault();

      var todo = {
         date: new Date().getTime(),
         title: this.state.title.trim(),
         description: this.state.description.trim(),
         done: false
      };

      if (!todo.title) {
         alert(this.i18n.errors.title);
         return;
      }

      if (!todo.description) {
         alert(this.i18n.errors.description);
         return; …
Run Code Online (Sandbox Code Playgroud)

reactjs jestjs enzyme

4
推荐指数
2
解决办法
5143
查看次数