我有相同代码的 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')
在测试块内部(或在任何函数内部)移动会导致错误吗?
当使用无论如何 crate时,错误可以方便地冒泡到应用程序的根目录,并在那里进行处理。
然而,有时我想知道错误发生在哪里,但我找不到一种方法来做到这一点anyhow
。
我的回溯仅提到了根:
4: mp_parser::main
at ./src/main.rs:37:5
Run Code Online (Sandbox Code Playgroud)
运行为RUST_BACKTRACE=full
我提供了详细的内部调用堆栈,但它没有显示我自己的代码中错误的根源。
因此,我经常对代码的不同部分取消注释,以找出错误实际发生的位置。
有什么办法可以得到它发生的原始行吗?
我已从使用actix-web
3.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) 我正在尝试将我的代码编译为 ES 代码(使用"type": "module"
内部 package.json 和"module": "esnext"
内部 tsconfig.json)。
我无法使用以下 3 种方法中的任何一种来运行它:
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 的问题。
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) 如果我有多个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)。我宁愿有一个更一般的功能。
有一个有点相关的线程在这里,但它铲球总结采摘选项来代替。
这是一个完整的菜鸟问题,但我无法弄清楚。当我输入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 完全陌生。
网上有很多关于如何执行以下操作的建议:
但我找不到有关如何在同一 VPC 内连接 2 个公共子网的任何信息。目前,我将它们连接到同一个 Internet 网关,但其中一个网关中的资源不与另一个网关通信。
解决这个问题的正确方法是什么?
我试图让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) 我正在尝试 tokio 的结果tokio::spawn
,tokio::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) 我有 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) rust ×5
javascript ×2
actix-web ×1
amazon-vpc ×1
closures ×1
cloud ×1
jestjs ×1
kubernetes ×1
node.js ×1
optional ×1
orm ×1
prisma ×1
rust-tokio ×1
ts-node ×1
typescript ×1
unit-testing ×1