gpt4 book ai didi

ruby-on-rails - 测试 RSpec 中的默认范围

转载 作者:行者123 更新时间:2023-11-28 19:41:44 24 4
gpt4 key购买 nike

我正在尝试在模型中测试我的默认范围线。

我的测试如下:

it 'orders by ascending name by default' do
expect(Coaster.scoped.to_sql).to eq Coaster.order(:name).to_sql
end

我的错误是:

expected: "SELECT \"coasters\".* FROM \"coasters\"  ORDER BY name ASC, name"
got: "SELECT \"coasters\".* FROM \"coasters\" ORDER BY name ASC"

错误第一行末尾的 , name 部分是什么意思,我该如何解决?

更新:

我的测试:

  describe 'default scope' do
let!(:coaster_one) { FactoryGirl.create(:coaster, name: "Tower of Terror") }
let!(:coaster_two) { FactoryGirl.create(:coaster, name: "Apocalypse") }

it 'orders by ascending name' do
Coaster.all.should eq [:coaster_two, :coaster_one]
end
end

我的错误:

expected: [:coaster_two, :coaster_one]
got: [#<Coaster id: 5, name: "Apocalypse", height: "60", speed: 60.0, length: "160", inversions: 4, material: nil, notes: nil, lat: nil, lng: nil, manufacturer_id: nil, park_id: 408, created_at: "2013-07-23 20:48:52", updated_at: "2013-07-23 20:48:52", slug: "apocalypse-at-alton-towers", style: nil, covering: nil, ride_style: nil, model: nil, layout: nil, order: nil, dates_ridden: nil, times_ridden: nil>, #<Coaster id: 4, name: "Tower of Terror", height: "60", speed: 60.0, length: "160", inversions: 4, material: nil, notes: nil, lat: nil, lng: nil, manufacturer_id: nil, park_id: 407, created_at: "2013-07-23 20:48:52", updated_at: "2013-07-23 20:48:52", slug: "tower-of-terror-at-alton-towers", style: nil, covering: nil, ride_style: nil, model: nil, layout: nil, order: nil, dates_ridden: nil, times_ridden: nil>]

(compared using ==)

更新 2:

看起来好像 Rails 4 不赞成使用 default_scope 所以鉴于此,我从我的模型中删除了 default_scope 并将其替换为标准范围。

新范围现在是:

scope :by_name_asc, lambda { order("name ASC") }

我的相关测试是:

  describe 'scopes' do
let!(:coaster_one) { FactoryGirl.create(:coaster, name: "Tower of Terror") }
let!(:coaster_two) { FactoryGirl.create(:coaster, name: "Apocalypse") }

it "orders coasters by ascending name" do
Coaster.by_name_asc.should eq [:coaster_two, :coaster_one]
end
end

运行此测试时我得到:

  1) Coaster scopes orders coasters by ascending name
Failure/Error: Coaster.by_name_asc.should eq [:coaster_two, :coaster_one]

expected: [:coaster_two, :coaster_one]
got: [#<Coaster id: 15, name: "Apocalypse", height: "60", speed: 60.0, length: "160", inversions: 4, material: nil, notes: nil, lat: nil, lng: nil, manufacturer_id: nil, park_id: 528, created_at: "2013-07-24 22:36:50", updated_at: "2013-07-24 22:36:50", slug: "apocalypse-at-alton-towers", style: nil, covering: nil, ride_style: nil, model: nil, layout: nil, order: nil, dates_ridden: nil, times_ridden: nil>, #<Coaster id: 14, name: "Tower of Terror", height: "60", speed: 60.0, length: "160", inversions: 4, material: nil, notes: nil, lat: nil, lng: nil, manufacturer_id: nil, park_id: 527, created_at: "2013-07-24 22:36:50", updated_at: "2013-07-24 22:36:50", slug: "tower-of-terror-at-alton-towers", style: nil, covering: nil, ride_style: nil, model: nil, layout: nil, order: nil, dates_ridden: nil, times_ridden: nil>]

(compared using ==)

Diff:
@@ -1,2 +1,2 @@
-[:coaster_two, :coaster_one]
+[#<Coaster id: 15, name: "Apocalypse", height: "60", speed: 60.0, length: "160", inversions: 4, material: nil, notes: nil, lat: nil, lng: nil, manufacturer_id: nil, park_id: 528, created_at: "2013-07-24 22:36:50", updated_at: "2013-07-24 22:36:50", slug: "apocalypse-at-alton-towers", style: nil, covering: nil, ride_style: nil, model: nil, layout: nil, order: nil, dates_ridden: nil, times_ridden: nil>, #<Coaster id: 14, name: "Tower of Terror", height: "60", speed: 60.0, length: "160", inversions: 4, material: nil, notes: nil, lat: nil, lng: nil, manufacturer_id: nil, park_id: 527, created_at: "2013-07-24 22:36:50", updated_at: "2013-07-24 22:36:50", slug: "tower-of-terror-at-alton-towers", style: nil, covering: nil, ride_style: nil, model: nil, layout: nil, order: nil, dates_ridden: nil, times_ridden: nil>]

# ./spec/models/coaster_spec.rb:10:in `block (3 levels) in <top (required)>'

有什么问题吗?

最佳答案

测试默认范围的更好方法是使用具有预期输出的真实数据。为您的类创建虚拟对象,然后查询类并将结果与​​您期望的正确顺序进行比较:

describe 'default scope' do
let!(:book_one) { Book.create(name: "The Count of Monte Cristo") }
let!(:book_two) { Book.create(name: "Animal Farm") }

it 'orders by ascending name' do
Book.all.should eq [book_two, book_one]
end
end

let!(:book_one) 创建一个 Book 的实例,并将其分配给名为 book_one 的局部变量。它的 name 属性是 The Count of Monte Cristolet!(:book_two) 做了类似的事情。

如果默认顺序是 name ASC,那么查询 Book 模型应该返回一个以 book_two 作为第一个元素的 ActiveRecord 关系("Animal...”),book_one 作为第二个元素(“The C...”)。

我们可以按如下方式测试这个期望:

Book.all.should eq [book_two, book_one]

这不是测试你的SQL,而是测试你代码的直接输出,比较有用。它也是独立于数据库的。

关于ruby-on-rails - 测试 RSpec 中的默认范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17817020/

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