From 7d496b55a89bce3f62fd7ced3dbd9c3a6fe99e1d Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 20 Apr 2016 17:25:47 -0400 Subject: [PATCH 1/2] Uses class eval to not lose context --- lib/shoulda/context/context.rb | 12 ++++++------ shoulda-context.gemspec | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/lib/shoulda/context/context.rb b/lib/shoulda/context/context.rb index be2232cc..59807d6f 100644 --- a/lib/shoulda/context/context.rb +++ b/lib/shoulda/context/context.rb @@ -396,9 +396,10 @@ def create_test_from_should_hash(should) end test_methods[test_unit_class][test_name.to_s] = true - + source_location = should[:block].source_location context = self - test_unit_class.send(:define_method, test_name) do + test_unit_class.class_eval <<-end_eval, source_location[0], source_location[1] + define_method test_name do @shoulda_context = context begin context.run_parent_setup_blocks(self) @@ -406,7 +407,6 @@ def create_test_from_should_hash(should) if self.respond_to?(:instance_exec) self.instance_exec(&should[:before]) else - # deprecated in Rails 4.x should[:before].bind(self).call end end @@ -414,13 +414,13 @@ def create_test_from_should_hash(should) if self.respond_to?(:instance_exec) self.instance_exec(&should[:block]) else - # deprecated in Rails 4.x should[:block].bind(self).call end ensure context.run_all_teardown_blocks(self) end - end + end + end_eval end def run_all_setup_blocks(binding) @@ -481,7 +481,7 @@ def test_name_prefix end def method_missing(method, *args, &blk) - test_unit_class.send(method, *args, &blk) + created_method = test_unit_class.send(method, *args, &blk) end end end diff --git a/shoulda-context.gemspec b/shoulda-context.gemspec index dd46d977..7c0cd5ee 100644 --- a/shoulda-context.gemspec +++ b/shoulda-context.gemspec @@ -24,4 +24,7 @@ Gem::Specification.new do |s| s.add_development_dependency("mocha", "~> 0.9.10") s.add_development_dependency("rake") s.add_development_dependency("test-unit", "~> 2.1.0") + s.add_development_dependency("pry") + s.add_development_dependency("byebug") + s.add_development_dependency("pry-byebug") end From f7ecc2e03b943fc372f1caf33225a0d69fc19702 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 21 Apr 2016 09:35:35 -0400 Subject: [PATCH 2/2] Refactor to make hound happy --- lib/shoulda/context/context.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/shoulda/context/context.rb b/lib/shoulda/context/context.rb index 59807d6f..cff68603 100644 --- a/lib/shoulda/context/context.rb +++ b/lib/shoulda/context/context.rb @@ -396,9 +396,9 @@ def create_test_from_should_hash(should) end test_methods[test_unit_class][test_name.to_s] = true - source_location = should[:block].source_location + file, line_no = should[:block].source_location context = self - test_unit_class.class_eval <<-end_eval, source_location[0], source_location[1] + test_unit_class.class_eval <<-end_eval, file, line_no define_method test_name do @shoulda_context = context begin @@ -481,7 +481,7 @@ def test_name_prefix end def method_missing(method, *args, &blk) - created_method = test_unit_class.send(method, *args, &blk) + test_unit_class.send(method, *args, &blk) end end end