Last active
December 12, 2015 13:48
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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