Skip to content

Instantly share code, notes, and snippets.

@rkellermeyer
Created August 6, 2013 13:36
Show Gist options
  • Save rkellermeyer/6164469 to your computer and use it in GitHub Desktop.
Save rkellermeyer/6164469 to your computer and use it in GitHub Desktop.
Scope that only returns users who are 21 or older
Failures:
1) User scopes .adult_purchases_enabled does not return users who are not at least 21 years old
Failure/Error: User.adult_purchases_enabled.should_not include(subject)
expected [#<User id: 338, first_name: "Romaine", last_name: "Mohr", address1: "87010 Derick Heights", address2: nil, city: "Welchton", state: "Arkansas", zip_code: "11561", avatar: nil, birth_date: "1993-08-06", email: "[email protected]", about: nil, created_at: "2013-08-06 13:31:54", updated_at: "2013-08-06 13:31:55", latitude: 40.5895, longitude: -73.6389, active: false>] not to include #<User id: 338, first_name: "Romaine", last_name: "Mohr", address1: "87010 Derick Heights", address2: nil, city: "Welchton", state: "Arkansas", zip_code: "11561", avatar: nil, birth_date: "1993-08-06 00:00:00", email: "[email protected]", about: nil, created_at: "2013-08-06 13:31:54", updated_at: "2013-08-06 13:31:55", latitude: 40.5895272, longitude: -73.6389038, active: false>
Diff:
@@ -1,2 +1,2 @@
-[#<User id: 338, first_name: "Romaine", last_name: "Mohr", address1: "87010 Derick Heights", address2: nil, city: "Welchton", state: "Arkansas", zip_code: "11561", avatar: nil, birth_date: "1993-08-06 00:00:00", email: "[email protected]", about: nil, created_at: "2013-08-06 13:31:54", updated_at: "2013-08-06 13:31:55", latitude: 40.5895272, longitude: -73.6389038, active: false>]
+[#<User id: 338, first_name: "Romaine", last_name: "Mohr", address1: "87010 Derick Heights", address2: nil, city: "Welchton", state: "Arkansas", zip_code: "11561", avatar: nil, birth_date: "1993-08-06", email: "[email protected]", about: nil, created_at: "2013-08-06 13:31:54", updated_at: "2013-08-06 13:31:55", latitude: 40.5895, longitude: -73.6389, active: false>]
# ./spec/models/user_spec.rb:80:in `block (4 levels) in <top (required)>'
2) User scopes .adult_purchases_enabled returns users who are at least 21 years old
Failure/Error: User.adult_purchases_enabled.should include(subject)
expected [] to include #<User id: 339, first_name: "Romaine", last_name: "Mohr", address1: "87010 Derick Heights", address2: nil, city: "Welchton", state: "Arkansas", zip_code: "11561", avatar: nil, birth_date: "1992-08-06 00:00:00", email: "[email protected]", about: nil, created_at: "2013-08-06 13:31:55", updated_at: "2013-08-06 13:31:55", latitude: 40.5895272, longitude: -73.6389038, active: false>
Diff:
@@ -1,2 +1,2 @@
-[#<User id: 339, first_name: "Romaine", last_name: "Mohr", address1: "87010 Derick Heights", address2: nil, city: "Welchton", state: "Arkansas", zip_code: "11561", avatar: nil, birth_date: "1992-08-06 00:00:00", email: "[email protected]", about: nil, created_at: "2013-08-06 13:31:55", updated_at: "2013-08-06 13:31:55", latitude: 40.5895272, longitude: -73.6389038, active: false>]
+[]
# ./spec/models/user_spec.rb:86:in `block (4 levels) in <top (required)>'
Finished in 11.66 seconds
34 examples, 2 failures
Failed examples:
rspec ./spec/models/user_spec.rb:77 # User scopes .adult_purchases_enabled does not return users who are not at least 21 years old
rspec ./spec/models/user_spec.rb:83 # User scopes .adult_purchases_enabled returns users who are at least 21 years old
class User < ActiveRecord::Base
# ...
scope :adult_purchases_enabled, -> { where('birth_date >= ?', 21.years.ago.midnight) }
# ...
end
describe User do
let(:user) { FactoryGirl.create(:valid_user) }
subject { user }
# ...
describe '.adult_purchases_enabled' do
it 'does not return users who are not at least 21 years old' do
subject.birth_date = 20.years.ago.midnight
subject.save!
User.adult_purchases_enabled.should_not include(subject)
end
it 'returns users who are at least 21 years old' do
subject.birth_date = 21.years.ago.midnight
subject.save!
User.adult_purchases_enabled.should include(subject)
end
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment