Skip to content

Instantly share code, notes, and snippets.

@kwiest
Last active December 12, 2015 13:48
Show Gist options
  • Save kwiest/4780313 to your computer and use it in GitHub Desktop.
Save kwiest/4780313 to your computer and use it in GitHub Desktop.
Error with lambda variable scope Not retaining local vars on creation Non rails version works as I expect
class EncryptedAttributesWithAfterCallbacksTest < MiniTest::Unit::TestCase
def setup
@password = ''
@ran_callback = false
callback = lambda { |record|
p 'Callback being called...'
@password = record.password
@ran_callback = true
}
User.encrypts :password, :after => callback
@user = User.create! :login => 'admin', :password => 'secret'
end
def test_should_run_callback
assert @ran_callback, 'Callback should have been called'
end
end
# Run options:
# Running tests:
# => "Callback being called..."
# F
# 1 test, 1 assertions, 1 failures, 0 errors, 0 skips
require 'minitest/autorun'
class NonRails
attr_reader :password
def initialize
@password = 'secret'
yield self
end
end
class NonRailsTest < MiniTest::Unit::TestCase
def setup
@password = ''
@ran_callback = false
callback = lambda { |record|
p 'Callback being called...'
@password = record.password
@ran_callback = true
}
NonRails.new &callback
end
def test_should_run_callback
assert @ran_callback, 'Callback should have been called'
end
end
# Run options:
# Running tests:
# => "Callback being called..."
# .
# 1 test, 1 assertions, 0 failures, 0 errors, 0 skips
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment