我试图弄清楚在使用 React 测试库测试下一个 js 应用程序时如何模拟对 auth0 身份验证后端的调用。我正在使用auth0/nextjs-auth0来处理身份验证。我的目的是使用MSW为所有 API 调用提供模拟。
我按照 nextjs 文档next.js/examples/with-msw中的示例为客户端和服务器 API 调用设置模拟。auth0/nextjs-auth0包(/api/auth/login、和)生成的所有 API 调用都/api/auth/callback收到模拟响应。/api/auth/logout/api/auth/me
模拟响应/api/auth/me如下所示
import { rest } from 'msw';
export const handlers = [
// /api/auth/me
rest.get(/.*\/api\/auth\/me$/, (req, res, ctx) => {
return res(
ctx.status(200),
ctx.json({
user: { name: 'test', email: 'email@domain.com' },
}),
);
}),
];
Run Code Online (Sandbox Code Playgroud)
当我在浏览器中运行该应用程序时,示例设置工作正常。但是当我运行测试时,模拟没有被拾取。
示例测试块如下所示
import React from 'react';
import {render , screen } from '@testing-library/react'; …Run Code Online (Sandbox Code Playgroud) 在阅读python文档时,我遇到了这个itertools.groupby()
函数.这不是很简单所以我决定在stackoverflow上查找一些信息.我从如何使用Python的itertools.groupby()中找到了一些东西?.
在这里和文档中似乎没有关于它的信息,所以我决定发表我的意见以征求意见.
谢谢
我正在尝试安装expo-cli.
当我使用时npm install -g expo-cli,它会安装以下输出。
当我尝试运行该expo命令时,出现系统找不到指定路径的错误。然后我卸载了npm uninstall -g expo-cli
我卸载node并npm清理了我的系统,然后我尝试expo-cli使用yarn global add expo-cli. 它安装了以下输出。
在此之后,expo init命令起作用了。但是当我expo start在项目文件夹中运行时,我得到以下输出。
我不知道还能做什么。我在 Windows 10 上。
目前,我的 django 项目中有一个 google Drive API 客户端,可以按预期工作。
import unittest
from unittest import mock
DRIVE_API_VERSION = "v3"
DRIVE_API_SERVICE_NAME = "drive"
DRIVE_AUTHORIZED_USER_FILE = "path/to/secrets/json/file"
DRIVE_SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.file ', 'https://www.googleapis.com/auth/drive.appdata']
def construct_drive_service():
try:
drive_credentials = google.oauth2.credentials.Credentials.from_authorized_user_file(
DRIVE_AUTHORIZED_USER_FILE, scopes=DRIVE_SCOPES)
except FileNotFoundError:
print('Drive credentials not created')
pass
if drive_credentials:
return build(DRIVE_API_SERVICE_NAME, DRIVE_API_VERSION, credentials=drive_credentials, cache_discovery=False)
else:
return None
Run Code Online (Sandbox Code Playgroud)
现在我面临的挑战是为这个函数编写测试。但我不知道该使用什么策略。我试过这个
class TestAPICalls(unittest.TestCase):
@mock.patch('api_calls.google.oauth2.credentials', autospec=True)
def setUp(self, mocked_drive_cred):
self.mocked_drive_cred = mocked_drive_cred
@mock.patch('api_calls.DRIVE_AUTHORIZED_USER_FILE')
def test_drive_service_creation(self, mocked_file):
mocked_file.return_value = "some/file.json"
self.mocked_drive_cred.Credentials.return_value = mock.sentinel.Credentials
construct_drive_service()
self.mocked_drive_cred.Credentials.from_authorized_user_file.assert_called_with(mocked_file)
Run Code Online (Sandbox Code Playgroud)
但我的测试失败并出现以下错误
with io.open(filename, 'r', …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用pipenv创建虚拟环境。当我给出命令pipenv install 时,出现以下错误
我正在使用pipenv==2018.6.25
回溯(最近一次调用):文件“c:\programdata\anaconda3\lib\site-packages\pipenv\vendor\requirementslib_compat.py”,第 33 行,在 do_import _tmp = importlib.import_module(internal) 文件“c:\ programdata\anaconda3\lib\importlib__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File “”,第 953 行,在 _find_and_load_unlocked ModuleNotFoundError 中:没有名为“pip._internal.basecommand”的模块
在处理上述异常的过程中,又发生了一个异常:
回溯(最近一次调用最后一次):文件“c:\programdata\anaconda3\lib\runpy.py”,第 193 行,在 _run_module_as_main “ main", mod_spec) 文件 "c:\programdata\anaconda3\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\ProgramData\Anaconda3\Scripts\pipenv.exe__main__.py", line 5、在文件“c:\programdata\anaconda3\lib\site-packages\pipenv__init__.py”第23行,从.cli导入cli文件“c:\programdata\anaconda3\lib\site-packages\pipenv\cli .py”,第 26 行,在 from .utils import is_valid_url 文件“c:\programdata\anaconda3\lib\site-packages\pipenv\utils.py”,第 59 行,在 from .vendor.requirementslib import Requirement …
所以我有一个像这样的pandas date_range
dates = pd.date_range(start='2005-1-1', end='2014-12-31', freq='D')
Run Code Online (Sandbox Code Playgroud)
我想删除因闰年而产生的所有额外天数.
我做一个for循环
for each in index:
if each.month==2 and each.day==29:
print(each) # I actually want to delete this item from dates
Run Code Online (Sandbox Code Playgroud)
但我的问题是我不知道如何删除该项目.常规的python列表方法和函数不起作用.我到处都看了.我查看了pandas.date_range的文档,但一无所获
任何帮助将不胜感激.
我有一个具有以下结构的流星集合。这实际上来自 的一个Meteor-files集合ostrio。
{
"_id" : "HsXoZ6bxkx5kMcJtm",
"name" : "trees.jpg",
"meta" : { "artist_id" : "QkmYdsZsMmRzqTg58" , "artist": "some name"},
"mime-type" : "audio/mp3",
"userId" : "QkmYdsZsMmRzqTg58",
"_collectionName" : "images"
}
Run Code Online (Sandbox Code Playgroud)
我定义一个出版物
Meteor.publish('files.artist', function publishUserImages(){
return Images.find({meta: {artist_id: this.userId}).cursor;
});
Run Code Online (Sandbox Code Playgroud)
我想过滤键meta,返回所有带有artist_id. 我当前的过滤器将仅获取meta具有确切值的那些图像{artist_id: "QkmYdsZsMmRzqTg58"}。过滤器不会返回上面显示的项目,因为它的meta值有一个额外的键artist
如何构建合适的过滤器?
我正在使用前端应用程序的onsen-ui样式。据我了解,管理按页面到堆栈的导航,其中每个页面都有唯一的标识符。meteorReactonsen-uipushing
这是我的页面的加载方式App.js
loadPage = (page) => {
const currentPage = this.navigator.pages.slice(-1)[0]
if(currentPage.key != page.name){
this.navigator.pushPage(
{
component: page,
props: { key: page.name }
},
{
animation: 'fade',
animationOptions: {duration: 0.3, timing: 'ease-in'}
},
);
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止一切正常。但我现在已经包含在我的项目中,所以我有一些通过的功能redux连接到商店的组件。react-reduxconnect()
问题是,对于每个connect包装的组件, name 属性都会变成Connect,因此onsen-ui必须处理堆栈中具有相同名称的多个页面。
举个例子,假设我有一个下面定义的组件
const ComponentName = props => {
return (
<p>Test component</p>
)
}
export default connect()(ComponentName)
Run Code Online (Sandbox Code Playgroud)
通常,ComponentName.name返回ComponentName,但一旦用 包裹connect …
meteor ×2
python ×2
date-range ×1
expo ×1
javascript ×1
mongodb ×1
msw ×1
next.js ×1
node.js ×1
npm ×1
onsen-ui ×1
pandas ×1
pipenv ×1
python-3.x ×1
react-native ×1
react-redux ×1
reactjs ×1
testing ×1