Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Resolve feature envy in subject by exposing the method it needs on

example.

- Use subscriptions to determine whether we're in the example block or
  not.
- This is a bit more code, but takes some of the noise out of the run()
  method.
  • Loading branch information...
commit 5e4dc33999df0691fb63271a83c3b4788234de63 1 parent ad5a76a
@dchelimsky dchelimsky authored
Showing with 11 additions and 9 deletions.
  1. +10 −7 lib/rspec/core/example.rb
  2. +1 −2  lib/rspec/core/subject.rb
View
17 lib/rspec/core/example.rb
@@ -1,8 +1,15 @@
module RSpec
module Core
class Example
+ RSpec.subscribe(:example_initialized) {|e| e.example.in_block = true}
+ RSpec.subscribe(:example_executed) {|e| e.example.in_block = false}
attr_reader :metadata, :example_block, :options
+ attr_accessor :in_block
+
+ def in_block?
+ !!in_block
@iromeo
iromeo added a note

It seems there is a typo. "!! in_block" is the same as "in_block"

@dchelimsky Owner

If in_block is nil (as opposed to false), then in_block? returns nil. By saying !!in_block, in_block? will return false whether the value of in_block is nil or false.

So I wouldn't call this a typo :) It's actually a fairly common idiom in Ruby predicates.

@iromeo
iromeo added a note

Ok, sounds reasonable, I forgot about nil here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
def self.delegate_to_metadata(*keys)
keys.each do |key|
@@ -24,14 +31,13 @@ def example_group
@example_group_class
end
- def in_block?
- @in_block
+ def specifies_attribute?
+ in_block && metadata[:attribute_of_subject]
end
alias_method :behaviour, :example_group
def run(example_group_instance, reporter)
- @in_block = false
@example_group_instance = example_group_instance
@example_group_instance.example = self
@@ -44,7 +50,6 @@ def run(example_group_instance, reporter)
run_before_each
reporter.example_initialized(@example_group_instance)
pending_declared_in_example = catch(:pending_declared_in_example) do
- @in_block = true
if @example_group_class.hooks[:around][:each].empty?
@example_group_instance.instance_eval(&example_block) unless pending
else
@@ -55,12 +60,11 @@ def run(example_group_instance, reporter)
rescue Exception => e
exception = e
ensure
- @in_block = false
+ reporter.example_executed(@example_group_instance)
assign_auto_description
end
begin
- reporter.example_executed(@example_group_instance)
run_after_each
rescue Exception => e
exception ||= e
@@ -125,7 +129,6 @@ def assign_auto_description
def record_results(results={})
execution_result.update(results)
end
-
end
end
end
View
3  lib/rspec/core/subject.rb
@@ -84,8 +84,7 @@ def attribute_of_subject
end
def using_attribute?
- example.in_block? &&
- example.metadata[:attribute_of_subject]
+ example.specifies_attribute?
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.