Guest User

Untitled

a guest
May 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. # spec/support/matchers/attribute.rb
  2. module AttributeMatchers
  3.  
  4. Spec::Matchers.define :have_only_attributes do |attributes|
  5. match do |target|
  6. actual_attributes = target.attribute_names.map(&:to_sym)
  7. diff = ((actual_attributes | attributes) - (actual_attributes & attributes))
  8. diff.length == 0
  9. end
  10. end
  11. end
  12.  
  13. # spec/spec_helper
  14. Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |file| require file }
  15.  
  16. Spec::Runner.configure do |config|
  17. config.include AttributeMatchers
  18. end
  19.  
  20. # app/models/user.rb
  21. class User < ActiveRecord::Base
  22. def self.method_returning_only_one_user
  23. first(:select => "first_name, last_name, email")
  24. end
  25. end
  26.  
  27. # spec/models/user_spec.rb
  28. User.method_returning_only_one_user.should have_only_attributes([:first_name, :last_name, :email])
Add Comment
Please, Sign In to add comment