public
Created

rspec bug?

  • Download Gist
rspec_bug.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
#
# Old version
#
class Foo
def initialize
@counter = 0
end
 
def false_if_first_time
if @counter == 0
@counter += 1
false
else
true
end
end
end
 
describe Foo do
subject { Foo.new }
its(:false_if_first_time) { should be_false } #=> its fail!
end
 
# because self#subject should be memorized by this code,
# @_subject ||= obj.its_method
# but when its_method returns false, its_method will called multiple times.
#
 
#
# fixed rspec-core/rspec/core/subject
#
module RSpec
module Core
module Subject
module ExampleGroupMethods
def its(attribute, &block)
describe(attribute) do
example do
self.class.class_eval do
define_method(:subject) do
if @_subject.nil?
@_subject = if attribute.is_a?(Array)
super()[*attribute]
else
attribute.to_s.split('.').inject(super()) do |target, method|
target.send(method)
end
end
end
@_subject
end
end
instance_eval(&block)
end
end
end
end
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.