gpt4 book ai didi

ruby-on-rails - 创建多个工厂来测试订单

转载 作者:行者123 更新时间:2023-11-28 21:29:47 25 4
gpt4 key购买 nike

我正在尝试测试工厂的订单。 (1) 我需要帮助编写测试工厂订单是否正确的测试。所以我假设我必须创建工厂然后更改结果然后对它们进行排序。

此外,(2) 我想知道是否有一种 DRYer 方法可以在工厂中创建多个项目。

spec/models/item_spec.rb

require 'rails_helper'

RSpec.describe Item, :type => :model do

let(:item) { FactoryGirl.create(:item) }
let(:item2) { FactoryGirl.create(:item) }
let(:item3) { FactoryGirl.create(:item) }

it "should sort the items in order" do
# tests order
end
end

spec/factories/items.rb

FactoryGirl.define do
factory :item, :class => 'Item' do
name { Forgery::Name.name }
sequence(:ordering)
end
end

最佳答案

在类似情况下,我使用 Ruby 的 .times 方法 ( docs ) 生成一定数量的 Factory 项,然后将对象映射到数组中:

# spec/model/item_spec.rb
require 'spec_helper'

describe Item do

let(:items) {3.times.map {FactoryGirl.create(:item)} } # An array of Items

it "should sort the items in order" do
# tests order
end
end

关于ruby-on-rails - 创建多个工厂来测试订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26921609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com