Skip to content

Instantly share code, notes, and snippets.

@vendethiel
Last active August 29, 2015 14:01
Show Gist options
  • Save vendethiel/3190490d3a3e900178c1 to your computer and use it in GitHub Desktop.
Save vendethiel/3190490d3a3e900178c1 to your computer and use it in GitHub Desktop.
Ruby's metaprogramming. I love it.
require 'test_helper'
class AbilityTest < ActiveSupport::TestCase
def setup
super
@ability_t = Ability.new(users(:translator_my))
@ability_m = Ability.new(users(:manager_my))
@ability_a = Ability.new(users(:admin_my))
@proj1 = projects(:proj1)
@proj2 = projects(:proj2)
end
module Translator
module Project
def can_read_a_project_they_have
assert @ability_t.can? :read, @proj1
end
end
module Translation
def test_translator__
end
end
end
module Manager
module Project
end
end
include DeepTestHelper
end
module DeepTestHelper
def self.included(me)
# I need mine to be 2 levels deep, you do you.
me.constants(false).each do |role|
me.const_get(role).constants.each do |model|
modul = me.const_get("#{role}::#{model}")
modul.instance_exec do
modul.instance_methods(false).each do |method|
alias_method "test_#{role}::#{model}::#{method}", method
remove_method method
end
end
me.send :include, modul
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment