Skip to content

Instantly share code, notes, and snippets.

@rwjblue
Last active December 17, 2015 20:29
Show Gist options
  • Save rwjblue/5667648 to your computer and use it in GitHub Desktop.
Save rwjblue/5667648 to your computer and use it in GitHub Desktop.
require 'rspec/autorun'
class Person
attr_accessor :first_name, :last_name, :gender
def full_name
"#{first_name} #{last_name}"
end
def self.average_age
23
end
private
def age
20
end
end
class Rockstar < Person; end
def visible?(klass, meth)
klass.respond_to?(meth) || klass.method_defined?(meth)
end
private :visible?
describe "visible?" do
context "Person" do
it "it should be true for accessor methods" do
visible?(Person, :first_name).should be_true
visible?(Person, :first_name=).should be_true
end
it "it should be true for instance method" do
visible?(Person, :full_name).should be_true
end
it "should be true for class method" do
visible?(Person, :average_age).should be_true
end
it "should be false for private method" do
visible?(Person, :age).should be_false
end
end
context "Rockstar" do
it "it should be true for accessor methods" do
visible?(Rockstar, :first_name).should be_true
visible?(Rockstar, :first_name=).should be_true
end
it "it should be true for instance method" do
visible?(Rockstar, :full_name).should be_true
end
it "should be true for class method" do
visible?(Rockstar, :average_age).should be_true
end
it "should be false for private method" do
visible?(Rockstar, :age).should be_false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment