How can i have rspec test for my default scope
Asked Answered
C

3

18

my model has default_scope(:order => 'created_at' ) my tests (rspec, factory girl, shoulda, etc.) are:

require 'spec/spec_helper.rb'

describe CatMembership do
  context "is valid" do
    subject { Factory.build(:cat_membership) }
    it { should be_valid }
    it { should belong_to :cat}
    it { should belong_to :cat_group}
    it { should have_db_column(:start_date)}
    it { should have_db_column(:end_date)}
  end
end
Cedar answered 28/7, 2011 at 3:18 Comment(0)
J
30

I would rather have this tested using a query and checking the results, but if you really must do it, one possible solution would be something like this for Rails 3:

CatMembership.scoped.to_sql.should == CatMembership.order(:created_at).to_sql

And on Rails 2:

CatMembership.default_scoping.should == [{:create=>{}, :find=>{:order=>"created_at"}}]

But I would not say these solutions are ideal since they show a lot of knowledge of the implementation (and you can see the implementation varies with different Rails versions).

Creating sample data, running an usual all query and checking the result is correctly ordered might have been simpler, would be closer to real unit testing and would work even as you upgrade your rails version.

In this case it would possibly be:

before do
  @memberships = []

  @memberships << CatMembership.create!
  @memberships << CatMembership.create!
  @memberships << CatMembership.create!

  [ 1.hour.ago, 5.minutes.ago, 1.minute.ago ].each_with_index do |time, index|
    membership = @memberships[index]
    membership.created_at = time
    membership.save
  end

end

it 'should be correctly ordered' do
  @sorted_memberships = CatMembership.all
  @memberships.first.should == @sorted_memberships.last
  @memberships.second.should == @sorted_memberships.second
  @memberships.third.should == @sorted_memberships.first
end

It's much more verbose, but it's going to work even as you move forward on rails.

And now I have just noticed who asked the question :D

Jubilate answered 28/7, 2011 at 3:54 Comment(6)
+1 - Mauricio's example specifies behavior, whereas Michael's example specifies structure.Orthodoxy
Hi David, great comment. I think I want to do both really. Have tests for the basic structure to make sure things don't get renamed, changed, etc. But also I would like to test whether it is really getting 'enforced' the way I have structured it, so Mauricio's example is great for that. I will also look to implement the first line Mauricio has given as that should help with structureCedar
I really like Mauricio's first line to address the structure. However when I have added context "scope is valid" do subject { GorillaMembership.scoped.to_sql == GorillaMembership.order(:created_at).to_sql } it {should be_valid} end I get NoMethodError: undefined method `valid?' for false:FalseClassCedar
There isn't a method available, you have to use that line as it is inside an it block.Burdine
Hi Mauricio I tried and tried to get it (the structure, scope_to) bit working but every combination gave issues. I will have to return to this in the future. Last try was: context "scope is valid" do pending { GorillaMembership.scoped.to_sql == GorillaMembership.order(:created_at).to_sql } it {should be_valid} endCedar
or rather: context "scope is valid" do subject { GorillaMembership.scoped.to_sql == GorillaMembership.order(:start_date).to_sql } it {should == true} endCedar
L
3

DRY It Up, Use Shared Examples

Most likely, you'll have more than one model with a similar default scope (if not, mostly ignore this method) so you can put this Rspec example into a shared_example where you can call it from a variety of model specs.

My preferred method of checking a default scope is to make sure the default ActiveRecord::Relation has the expected clause (order or where or whatever the case may be), like so:

spec/support/shared_examples/default_scope_examples.rb

shared_examples_for 'a default scope ordered by created_at' do

  it 'adds a clause to order by created_at' do
    described_class.scoped.order_clauses.should include("created_at")
  end

end

And then in your CatMembership spec (and any other spec that has the same default scope), all you need to is:

spec/models/cat_membership_spec.rb

describe CatMembership

  it_behaves_like 'a default scope ordered by created_at'

  # other spec examples #

end

Finally, you can see how this pattern can be extended to all sorts of default scopes and keeps things clean, organized and, best of all, DRY.

Lietman answered 19/12, 2014 at 19:17 Comment(0)
A
0

According to latest conventions of RSpec expect is recommended instead of should so, better answer would be

expect(CatMembership.scoped.to_sql).to eq(CatMembership.order(:created_at).to_sql)

However, lately Model.scoped is deprecated so it is recommended to use Model.all instead

expect(CatMembership.all.to_sql).to eq(CatMembership.order(:created_at).to_sql)

Agouti answered 14/6, 2015 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.