Skip to content

Instantly share code, notes, and snippets.

@rafaelss
Created May 13, 2010 15:11
Show Gist options
  • Save rafaelss/399926 to your computer and use it in GitHub Desktop.
Save rafaelss/399926 to your computer and use it in GitHub Desktop.
# spec/support/matchers/attribute.rb
module AttributeMatchers
Spec::Matchers.define :have_only_attributes do |attributes|
match do |target|
actual_attributes = target.attribute_names.map(&:to_sym)
diff = ((actual_attributes | attributes) - (actual_attributes & attributes))
diff.length == 0
end
end
end
# spec/spec_helper
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |file| require file }
Spec::Runner.configure do |config|
config.include AttributeMatchers
end
# app/models/user.rb
class User < ActiveRecord::Base
def self.method_returning_only_one_user
first(:select => "first_name, last_name, email")
end
end
# spec/models/user_spec.rb
User.method_returning_only_one_user.should have_only_attributes([:first_name, :last_name, :email])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment