如何axios
将该导出模拟为默认函数?
我有api帮助器,概括了api请求 axios()
api.js
export const callApi = (endpoint, method, data = {}) => {
return axios({
url: endpoint,
method,
data
})
.then((response) => // handle response)
.catch((error) => // handle error)
};
Run Code Online (Sandbox Code Playgroud)
api.spec.js
import axios from 'axios';
import { callApi } from './api';
describe('callApi()', () => {
it('calls `axios()` with `endpoint`, `method` and `body`', () => {
// mock axios()
jest.spyOn(axios, 'default');
const endpoint = '/endpoint';
const method = 'post';
const data = { foo: 'bar' }; …
Run Code Online (Sandbox Code Playgroud) 我的项目有 2 个相互引用的模型。当一个模型的实例被删除时,remove()
方法将挂钩另一个模型以删除依赖项。
照片模型.js
const Album = require('./album');
.
.
// post hook of photo.remove()
schema.post('remove', (photo, next) => {
console.log(Album); // return empty obj {}
Album.findById(photo._album, (error, album) => {
// find album and remove photo
});
});
Run Code Online (Sandbox Code Playgroud)
删除钩子内的相册模型返回空对象。我通过在钩子内移动 require 语句找到了修复。
schema.post('remove', (photo, next) => {
const Album = require('./album');
Album.findById(photo._album, (error, album) => {
// find album and remove photo
});
});
Run Code Online (Sandbox Code Playgroud)
但是修复对我来说看起来很难看,我的猜测是每次photo.remove()
调用require
语句都会被调用。
题:
require
每次photo.remove()
被调用时都被调用”的猜测是否正确?require
外面的钩子和里面的行为一样? …我的目标是“仅在 a、b 和 c 相等时执行代码”。我想出了两个代码:
代码#1:
if (a===b===c) {console.log('something')};
Run Code Online (Sandbox Code Playgroud)
代码#2:
if ( (a===b)&&(a===c)&&(b===c) ) {console.log('something')};
Run Code Online (Sandbox Code Playgroud)
我已经尝试过两者并意识到只有“code#2”能够响应我的目的(仅当 3 个变量等效时才执行(例如a=b=c
),但是对于“code#1”,只要有 2 个等效变量(例如.a=b
或b=c
..)
我的问题是:“代码#1 和代码#2 之间有什么区别?”
请参阅此函数 function 的输出。它表明静态函数可以被覆盖,因为派生类继承了函数:
void put(){printf("Static functions in base class");}
Run Code Online (Sandbox Code Playgroud)
如果我们不覆盖put()
输出是基类中的静态函数,但我们将其覆盖为:
void put(){printf("Static functions are overridden in derived class");}
Run Code Online (Sandbox Code Playgroud)
所以输出是静态函数在派生类 public 中被覆盖:
#include<iostream>
class base{
public:
static void put(){printf("Static functions in base class");}
};
class derived : public base{
void put(){printf("Static functions are overridden in derived
class");}
};
int main(){
derived *bp = new derived;// Static Polymorphism //
bp->put();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于这里put()
不是虚函数。所以我们可以覆盖不是虚拟的函数吗?
这是静态多态的情况吗?
我正在测试apiMiddleware
调用它的辅助函数callApi
。为了防止调用callApi
将发出 API 调用的实际调用,我模拟了该函数。但是,它仍然被调用。
apiMiddleware.js
import axios from 'axios';
export const CALL_API = 'Call API';
export const callApi = (...arg) => {
return axios(...arg)
.then( /*handle success*/ )
.catch( /*handle error*/ );
};
export default store => next => action => {
// determine whether to execute this middleware
const callAPI = action[CALL_API];
if (typeof callAPI === 'undefined') {
return next(action)
}
return callAPI(...callAPI)
.then( /*handle success*/ )
.catch( /*handle error*/ );
}
Run Code Online (Sandbox Code Playgroud)
apiMiddleware.spec.js
import …
Run Code Online (Sandbox Code Playgroud) 我们如何在删除请求后动态重定向回同一页面。
请求链接:
出现在很多页面的请求链接。
<a href="/delete/id">delete</a>
Run Code Online (Sandbox Code Playgroud)
路由.js
router.get('/delete/:id', function (req, res, next) {
// deleting logic here
res.redirect('/to/requesting/page');
});
Run Code Online (Sandbox Code Playgroud)
/to/resquesting/page
请求删除的页面的 url在哪里:
/delete/:id
已从主页请求,我们将致电res.redirect('/home');
/delete/:id
已从产品请求,我们会打电话res.redirect('/product);
javascript ×3
jestjs ×2
node.js ×2
axios ×1
c++ ×1
express ×1
inheritance ×1
mocking ×1
mongoose ×1
polymorphism ×1
redirect ×1
static ×1
virtual ×1