假设我有一个导出默认功能的简单文件:
// UniqueIdGenerator.js
const uniqueIdGenerator = () => Math.random().toString(36).substring(2, 8);
export default uniqueIdGenerator;
Run Code Online (Sandbox Code Playgroud)
我会这样使用:
import uniqueIdGenerator from './UniqueIdGenerator';
// ...
uniqueIdGenerator();
Run Code Online (Sandbox Code Playgroud)
我想在测试中断言在保持原始功能的同时调用了此方法。jest.spyOn
但是,我需要一个对象以及一个函数名称作为参数。您如何以一种干净的方式做到这一点?还有一个类似的GitHub的问题为jasmine
兴趣的人。
我想从 GitHub Actions 控制 Amplify 部署,因为 Amplify 自动构建
cypress
开箱即用地运行作业我在Ruby中处理一个巨大的JSON文件时遇到了麻烦.我正在寻找的是一种逐个处理它的方法,而不会在内存中保留太多数据.
我认为yajl-ruby gem会做这项工作,但它会消耗我所有的记忆.我也看过Yajl :: FFI和JSON:流宝石,但有明确说明:
对于较大的文档,我们可以使用IO对象将其流式传输到解析器中.我们仍然需要解析对象的空间,但文档本身永远不会完全读入内存.
这是我用Yajl做的事情:
file_stream = File.open(file, "r")
json = Yajl::Parser.parse(file_stream)
json.each do |entry|
entry.do_something
end
file_stream.close
Run Code Online (Sandbox Code Playgroud)
内存使用量持续增加,直到进程被终止.
我不明白为什么Yajl会在内存中保留已处理的条目.我可以以某种方式释放它们,还是我误解了Yajl解析器的功能?
如果无法使用Yajl完成:有没有办法在Ruby中通过任何库?
我正在尝试在我的项目中实现 GraphQL,我想passport.authenticate('local')
在我的登录中使用Mutation
我想要的代码改编:
const typeDefs = gql`
type Mutation {
login(userInfo: UserInfo!): User
}
`
const resolvers = {
Mutation: {
login: (parent, args) => {
passport.authenticate('local')
return req.user
}
}
Run Code Online (Sandbox Code Playgroud)
问题:
passport
设计多为REST / Express吗?passport.authenticate
方法(将用户名和密码传递给它)吗?我正在测试不应该在本地运行并且需要模拟的功能window.location.href
:
const usePageTracking = (): void => {
const location = useLocation();
useEffect(() => {
if (!window.location.href.includes("localhost")) {
ReactGA.initialize("UA-000000-01");
ReactGA.pageview(window.location.pathname + window.location.search);
}
}, []);
};
Run Code Online (Sandbox Code Playgroud)
在我的测试中:
describe("usePageTracking", () => {
it("initializes ReactGA", () => {
render(<Example />);
expect(ReactGA.initialize).toHaveBeenCalled();
});
it("tracks page view", () => {
render(<Example />);
expect(ReactGA.pageview).toHaveBeenCalledWith("/");
});
});
Run Code Online (Sandbox Code Playgroud)
注意:关于 Vue有一个相关的问题,但我不清楚这些解决方案是否也适用于 React(有些解决方案不起作用)。
我想在我的 docker-compose 文件中设置一个标志,如果我不需要我的数据库卷是否不会在容器外部持久化,但我仍然想要一个卷条目,如果我的数据库很小,那么我不希望它在容器外部持久化。
例如,可以在 Ruby 脚本中内联使用 ActiveRecord。我非常喜欢用它来报告错误、测试功能和分享要点。
我想知道是否可以为 Rails 网络服务做同样的事情?(我主要对让控制器层工作感兴趣,其余的应该很容易按需添加。)大致如下:
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
gem 'rails', '~> 6.0.0'
end
require 'rails/commands'
APP_PATH = File.expand_path('config/application', __dir__)
Rails::Command.invoke('server')
Run Code Online (Sandbox Code Playgroud)
在玩弄这个时,似乎确实需要一个外部入口点 ( APP_PATH
)。因此,或者,一种可接受的方法是将所有配置塞入单个入口点。到目前为止,我无法让它发挥作用。
到目前为止,Rails 初始化过程是我找到的最好的资源。
我制作了一个最小的 Rails 应用程序,如下所示:
rails new min-rails --skip-keeps --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-record --skip-active-storage --skip-puma --skip-action-cable --skip-sprockets --skip-spring --skip-listen --skip-javascript --skip-turbolinks --skip-test --skip-system-test --skip-bootsnap --api
cd min-rails …
Run Code Online (Sandbox Code Playgroud) 另一个 OAuth2 问题在其他地方没有完全涵盖。
我使用 NestJS 后端、React 前端、Passport 和我自己的数据库进行身份验证。尝试添加 OAuth2 身份提供商 (Google)。
我将 NestJS 应用程序配置为 OAuth 客户端。登录后,我收到回调,此时我有一个access_token
从id_token
. 这被封装在一个扩展类中PassportStrategy(Strategy, 'google')
,并且AuthGuard('google')
其中大部分是自动处理的。代码在这里。
然而,此时,我需要在后端 (NestJS) 和前端 (React) 之间维护经过身份验证的会话。我想我需要一个 JWT,但我想知道最好的方法:
id_token
或access_token
)吗?这样我就不用担心自己发行代币了。即前端接收令牌(从后端或直接 IdP),在每个请求上发送它,后端在每个请求(verifyIdToken
或getTokenInfo
)上使用 IdP 验证它google-auth-library
。这里的一个缺点是每次都会产生额外的网络请求。我不确定是否有必要这样做,因为 IdP 仅用于识别用户,而不用于访问其他资源。另一个缺点是我需要存储刷新令牌并在令牌过期时进行处理(获取新令牌,在前端更新它)。
我知道关于这些主题的问题很多,但是我没有找到涵盖所有方面的问题。
考虑User
,Activity
和Like
模型。当我查询一个活动时,我希望为集合中的每个活动加载第一个“赞”,而不会进行N + 1个查询,也不会加载过多的必要记录。我的代码如下所示:
class User < ActiveRecord::Base
has_many :likes, as: :liker
end
class Activity < ActiveRecord::Base
has_many :likes
has_one :first_like, -> { order(created_at: :asc) }, class_name: "Like"
end
class Like < ActiveRecord::Base
belongs_to :activity
belongs_to :liker, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
我做了一个综合的要点来测试不同的加载策略和方法:https : //gist.github.com/thisismydesign/b08ba0ee3c1862ef87effe0e25386267
策略:N + 1个查询,左外部联接,单个额外查询
方法:eager_load
,includes
,includes & references
,includes & preload
(这将导致主要是左外连接或单额外的查询)
这是我发现的问题:
order(created_at: :asc)
联接不尊重关联范围default_scope { order(created_at: :asc) }
(请参阅:rails …如何在 Material-UI 中测试响应式元素?
例子:
import React from "react";
import Hidden from "@material-ui/core/Hidden";
const HideOnMobile = (props) => {
return <Hidden xsDown>{props.children}</Hidden>;
};
Run Code Online (Sandbox Code Playgroud)
测试用例:
describe(HideOnMobile, () => {
describe("when screensize is sm", () => {
it("shows children", () => {});
});
describe("when screensize is xs", () => {
it("hides children", () => {});
});
});
Run Code Online (Sandbox Code Playgroud) unit-testing reactjs material-ui enzyme react-testing-library
reactjs ×3
jestjs ×2
mocking ×2
passport.js ×2
ruby ×2
unit-testing ×2
activerecord ×1
aws-amplify ×1
database ×1
deployment ×1
docker ×1
enzyme ×1
express ×1
google-oauth ×1
graphql ×1
json ×1
material-ui ×1
memory ×1
nestjs ×1
oauth-2.0 ×1
parsing ×1
react-hooks ×1
spy ×1
testing ×1
yajl ×1