在 Rspec 中,我正在测试包含如下数组的哈希:subject { describe_class.call(build: build_one, user:)it '返回正确的数据' do response = subject expected_response = ...
在 Rspec 中,我正在测试包含如下数组的哈希:
subject { described_class.call(build: build_one, user:)
it 'returns the correct data' do
response = subject
expected_response = {
id: build_one.id,
status: {
key: 'active',
name: 'Active'
},
build_type: 'SystemBuild',
calculated_type_id: CalculatedType.standard.id,
build_time: build_one.build_time,
summary: 'Lorem ipsum dolor sit amet...',
data: [
{id: '1', name: '', parent: ''},
{id: '1.1', name: 'Calc type 1', parent: '1'},
{id: '1.2', name: 'Calc type 2', parent: '1'},
{id: '1.3', name: 'Calc type 3', parent: '1'},
{id: '1.1.1', name: 'Value 1', parent: '1.1'},
{id: '1.1.2', name: 'Value 2', parent: '1.1'},
{id: '1.2.1', name: 'Value 3', parent: '1.2'},
{id: '1.3.1', name: 'Value 4', parent: '1.3'},
{id: '1.3.2', name: 'Value 5', parent: '1.3'},
{id: '1.3.3', name: 'Value 6', parent: '1.3'}
]
}
expect(response).to match(expected_response)
end
我的问题是 data
数组不一定按照我在 expected_response
.
我当然可以更改 中的顺序 expected_response
,但我不想以这种方式限制它。对于应用程序来说,数组顺序根本不重要,更改应用程序的其他部分可能会改变顺序。唯一重要的是所有元素都存在于数组中。
当我运行测试时,我得到以下结果:
Diff:
:build_time: 2024-06-22 10:17:55.0000000000 -0700,
:build_type: "SystemType",
:calculated_type_id: 3,
:id => 234,
-:data => [{:id=>"1", :name=>"", :parent=>""}, {:id=>"1.1", :name=>"Calc type 1", :parent=>"1"}, {:id=>"1.2", :name=>"Calc type 2", :parent=>"1"}, {:id=>"1.3", :name=>"Calc type 3", :parent=>"1"}, {:id=>"1.1.1", :name=>"Value 1", :parent=>"1.1"}, {:id=>"1.1.2", :name=>"Value 2", :parent=>"1.1"}, {:id=>"1.2.1", :name=>"Value 3", :parent=>"1.2"}, {:id=>"1.3.1", :name=>"Value 4", :parent=>"1.3"}, {:id=>"1.3.2", :name=>"Value 5", :parent=>"1.3"}, {:id=>"1.3.3", :name=>"Value 6", :parent=>"1.3"}]
+:data => [{:id=>"1", :name=>"", :parent=>""}, {:id=>"1.1", :name=>"Calc type 1", :parent=>"1"}, {:id=>"1.2", :name=>"Calc type 2", :parent=>"1"}, {:id=>"1.3", :name=>"Calc type 3", :parent=>"1"}, {:id=>"1.1.1", :name=>"Value 1", :parent=>"1.1"}, {:id=>"1.2.1", :name=>"Value 3", :parent=>"1.2"}, {:id=>"1.3.2", :name=>"Value 5", :parent=>"1.3"}, {:id=>"1.1.2", :name=>"Value 2", :parent=>"1.1"}, {:id=>"1.3.1", :name=>"Value 4", :parent=>"1.3"}, {:id=>"1.3.3", :name=>"Value 6", :parent=>"1.3"}]
:status => {:key=>"active", :name=>"Active"},
:summary => "Lorem ipsum dolor sit amet..."
我认为这样 match(...)
可以忽略数组的顺序 data
,只需检查所有元素是否存在。有没有办法做到这一点而不必检查哈希中的每个元素?
我使用 nextjsi 时不断收到错误 OverwriteModelError:编译后无法覆盖 AddOnsDatabase 模型。为了修复此错误,我使用 OR 运算符来检查模型是否已存在
我正在使用 nextjs
我一直收到错误
OverwriteModelError: Cannot overwrite AddOnsDatabase model once compiled.
为了修复此错误,我使用 OR 运算符来检查模型是否已存在
import mongoose from "mongoose";
const AddOnsDatabaseSchema = new mongoose.Schema({
name: String,
limit: String,
required: Boolean,
});
const AddOnsDatabase =
mongoose.model("AddOnsDatabase") ||
mongoose.model("AddOnsDatabase", AddOnsDatabaseSchema);
export default AddOnsDatabase;
这修复了错误,但弄乱了打字稿,就像我使用时一样
const AddOnsDatabase = mongoose.model("AddOnsDatabase", AddOnsDatabaseSchema);
并像这样使用该模型:
"use server";
import { connectDB } from "@/lib/mongodb";
import AddOnsDatabase from "@/models/AddOnsDatabase";
import { DBAddon } from "@/types/temp";
export async function GetAddOns(): Promise<DBAddon[]> {
await connectDB();
const addons = await AddOnsDatabase.find().lean();
console.log(addons);
if (!addons) return [];
const addonsToReturn = addons.map((addon) => {
return {
id: addon._id ? addon._id.toString() : "",
name: addon.name ? addon.name : "",
limit: addon.limit ? addon.limit : "",
required: addon.required ? addon.required : false,
};
});
return addonsToReturn;
}
addons
会有这种类型
const addons: (FlattenMaps<{
name?: string | null | undefined;
limit?: string | null | undefined;
required?: boolean | null | undefined;
}> & {
_id: Types.ObjectId;
})[]
但使用 OR 运算符后 const AddOnsDatabase = mongoose.model("AddOnsDatabase") || mongoose.model("AddOnsDatabase", AddOnsDatabaseSchema);
addons
有类型
const addons: (FlattenMaps<any> & Required<{
_id: unknown;
}>)[]
请帮我找到一种方法来避免出现该错误并使用 Model.findById() 自动推断类型
我已经尝试过上述方法