Create a gist now

Instantly share code, notes, and snippets.

rspec bug?
#
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment