小编ilm*_*moi的帖子

开玩笑嘲笑:TypeError:axios.get.mockResolvedValue 不是函数

我有相同代码的 2 个版本,一种有效,一种抛出:

TypeError: axios.get.mockResolvedValue is not a function
Run Code Online (Sandbox Code Playgroud)

作品:

const axios = require('axios')
jest.mock('axios') //<<<------

test('should mock axios', async () => {
  const resp = {data: {moreData: 'zedata'}}
  axios.get.mockResolvedValue(resp)
  const actualresp = await getAxios()
  expect(actualresp).toEqual({moreData: 'zedata'})
})
Run Code Online (Sandbox Code Playgroud)

不:

const axios = require('axios')

test('should mock axios', async () => {
  jest.mock('axios') //<<<------
  const resp = {data: {moreData: 'zedata'}}
  axios.get.mockResolvedValue(resp)
  const actualresp = await getAxios()
  expect(actualresp).toEqual({moreData: 'zedata'})
})
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我理解为什么jest.mock('axios')在测试块内部(或在任何函数内部)移动会导致错误吗?

javascript unit-testing jestjs

30
推荐指数
1
解决办法
4万
查看次数

如何在使用“anyhow”时跟踪错误

当使用无论如何 crate时,错误可以方便地冒泡到应用程序的根目录,并在那里进行处理。

然而,有时我想知道错误发生在哪里,但我找不到一种方法来做到这一点anyhow

我的回溯仅提到了根:

   4: mp_parser::main
             at ./src/main.rs:37:5
Run Code Online (Sandbox Code Playgroud)

运行为RUST_BACKTRACE=full我提供了详细的内部调用堆栈,但它没有显示我自己的代码中错误的根源。

因此,我经常对代码的不同部分取消注释,以找出错误实际发生的位置。

有什么办法可以得到它发生的原始行吗?

error-handling rust

19
推荐指数
1
解决办法
7903
查看次数

Rust actix-web:特征 `Handler&lt;_, _&gt;` 未实现

我已从使用actix-web3.xx 转向 4.xx,之前运行良好的代码现在抛出此错误:

the trait bound `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}: Handler<_, _>` is not satisfied
  --> src/routes/all_routes.rs:74:14
   |
74 | pub async fn tweets4(
   |              ^^^^^^^ the trait `Handler<_, _>` is not implemented for `fn(actix_web::web::Query<TweetParams>, actix_web::web::Data<Pool<Postgres>>) -> impl std::future::Future {tweets4}`
Run Code Online (Sandbox Code Playgroud)

经过一番谷歌搜索后,似乎生态系统中确实存在一个Handler特征actix(但是,我认为不是 actix-web)。

我不知道我需要在哪里实现该特征。错误消息似乎表明函数本身缺少它,但我的理解是你只能在structs和上实现特征enums,而不是函数?

这是处理程序代码:

#[get("/tweets4")]
pub async fn tweets4(
    form: web::Query<TweetParams>,
    pool: web::Data<PgPool>,
) -> Result<HttpResponse, HttpResponse> {
    let fake_json_data = r#"
    { "name": "hi" }
    "#; …
Run Code Online (Sandbox Code Playgroud)

rust actix-web

9
推荐指数
2
解决办法
5703
查看次数

无法运行 TS 节点 + ES 应用程序 - 要么得到“ERR_UNKNOWN_FILE_EXTENSION”或“new ERR_MODULE_NOT_FOUND”

我正在尝试将我的代码编译为 ES 代码(使用"type": "module"内部 package.json 和"module": "esnext"内部 tsconfig.json)。

我无法使用以下 3 种方法中的任何一种来运行它:

  1. ts-node src/server.ts结果是:
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/ilmoi/Dropbox/crypto_bc/dbricks/mvp/server/src/server.ts
    at new NodeError (node:internal/errors:363:5)
    at Loader.defaultGetFormat [as _getFormat] (node:internal/modules/esm/get_format:71:15)
    at Loader.getFormat (node:internal/modules/esm/loader:105:42)
    at Loader.getModuleJob (node:internal/modules/esm/loader:243:31)
    at Loader.import (node:internal/modules/esm/loader:177:17)
    at Object.loadESM (node:internal/process/esm_loader:68:5)
Run Code Online (Sandbox Code Playgroud)

这个线程来看,这似乎是 ts-node 的问题。

  1. 所以我尝试node --loader ts-node/esm ./src/server.ts得到这个:
(node:45543) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the …
Run Code Online (Sandbox Code Playgroud)

node.js typescript ts-node

6
推荐指数
2
解决办法
3733
查看次数

选择第一个 Some() 选项的惯用方式?

如果我有多个Option<T>' 并且我想选择第一个Some而不是None- 有没有一种惯用的方法来做到这一点?

我天真的方法:

pub fn pick_first_option_available<T>(a: Option<T>, b: Option<T>, c: Option<T>) -> Option<T> {
    match a {
        Some(a) => Some(a),
        None => match b {
            Some(b) => Some(b),
            None => match c {
                Some(c) => Some(c),
                None => None,
            },
        },
    }
}
Run Code Online (Sandbox Code Playgroud)

上述的一个明显问题是它仅限于固定数量的选项 (3)。我宁愿有一个更一般的功能。

有一个有点相关的线程在这里,但它铲球总结采摘选项来代替。

optional rust

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

kubectl - 什么是客户端与服务器?

这是一个完整的菜鸟问题,但我无法弄清楚。当我输入kubectl version终端时,我得到:

Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.8", GitCommit:"ec6eb119b81be488b030e849b9e64fda4caaf33c", GitTreeState:"clean", BuildDate:"2020-03-12T21:00:06Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"darwin/amd64"}
Server Version: version.Info{Major:"1", Minor:"18", GitVersion:"v1.18.0", GitCommit:"9e991415386e4cf155a24b1da15becaa390438d8", GitTreeState:"clean", BuildDate:"2020-03-25T14:50:46Z", GoVersion:"go1.13.8", Compiler:"gc", Platform:"linux/amd64"}
Run Code Online (Sandbox Code Playgroud)

究竟是什么client,并server在kubectl的背景下?前者用于访问远程集群,后者用于在您自己的机器上运行集群?

还有一个问题:如何降级服务器的版本?我按照官方文档中的说明进行操作,但这只会更改服务器版本。通过谷歌搜索,我了解到服务器版本是由 Docker 设置的?我如何更改它+如果我这样做会破坏docker吗?

对不起,如果愚蠢的问题 - 我对 k8s 完全陌生。

kubernetes

4
推荐指数
1
解决办法
1612
查看次数

AWS:如何将2个公共子网连接在一起

网上有很多关于如何执行以下操作的建议:

  • 将公共子网连接到私有子网
  • 将 2 个独立的VPC连接在一起

但我找不到有关如何在同一 VPC 内连接 2 个公共子网的任何信息。目前,我将它们连接到同一个 Internet 网关,但其中一个网关中的资源不与另一个网关通信。

解决这个问题的正确方法是什么?

cloud amazon-web-services amazon-vpc

4
推荐指数
1
解决办法
8932
查看次数

将非复制变量移动到异步闭包中:捕获的变量无法转义 `FnMut` 闭包主体

我试图让clokwerk安排一个异步函数每 X 秒运行一次。

文档显示了这个例子:

// Create a new scheduler
let mut scheduler = AsyncScheduler::new();
// Add some tasks to it
scheduler
    .every(10.minutes())
        .plus(30.seconds())
    .run(|| async { println!("Simplest is just using an async block"); });
// Spawn a task to run it forever
tokio::spawn(async move {
  loop {
    scheduler.run_pending().await;
    tokio::time::sleep(Duration::from_millis(100)).await;
  }
});
Run Code Online (Sandbox Code Playgroud)

我的初步尝试:

    let config2 = // define a Config struct, Config
    let pg_pool2 = // get a sqlx connection pool, Pool<Postgres>

    //I assume I need shared references …
Run Code Online (Sandbox Code Playgroud)

closures rust

4
推荐指数
1
解决办法
94
查看次数

意外的 tokio::task::spawn_blocking 行为

我正在尝试 tokio 的结果tokio::spawntokio::task::spawn结果我不明白后者的行为方式。

当我运行以下代码时:

#[tokio::main]
pub async fn main() {
    // I'm spawning one block of functions
    let h = tokio::task::spawn_blocking(move || {
        block_one();
    });

    // and another block of functions
    let h2 = tokio::spawn(async move {
        block_two().await;
    });

    // then I collect the handles
    h.await.unwrap();
    h2.await.unwrap();
}

#[tokio::main] //needed as this block is not treated as syncronous by main
pub async fn block_one() {
    let mut handles = vec![];

    for i in 1..10 {
        let …
Run Code Online (Sandbox Code Playgroud)

rust rust-tokio

4
推荐指数
1
解决办法
4243
查看次数

Prisma js ORM - 如何过滤相关表中包含条目的结果(有效连接)?

我有 2 张桌子:

model Collection {
    id                String                 @id @default(uuid()) @db.Uuid/
    floorPrices       CollectionFloorPrice[]
}

model CollectionFloorPrice {
    id           String     @id @default(uuid()) @db.Uuid
    collection   Collection @relation(fields: [collectionId], references: [id])
    collectionId String     @db.Uuid
}
Run Code Online (Sandbox Code Playgroud)

如何查询仅包含行的集合CollectionFloorPrice?在 SQL 中,这将是一个简单的 JOIN。

这不起作用:

    return await this.prisma.collection.findMany({
      where: {
        floorPrices: {
          exists: true,
        },
      },
    });
Run Code Online (Sandbox Code Playgroud)

javascript orm prisma

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