我正在尝试在模型中测试我的默认范围线。
我的测试如下:
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)>'
关于出了什么问题的任何想法?