#######
test.rb
#######

#!/usr/bin/env ruby

class Thing
  def self.count
    @count ||= 0
  end

  def self.count=(val)
    @count += val
  end

  def self.reset_count
    @count = 0
  end

  def initialize
    self.class.count += 1
  end
end

###########
test_spec.rb
###########

require_relative "../test.rb"

describe Thing do
  after(:example) do
    puts "called" 
    #Thing.reset_count
  end

  context "using subject!" do
    subject!(:thing) { Thing.new }

    it "returns 1" do
      expect(Thing.count).to eq(1)
    end

    it "returns 2" do
      expect(Thing.count).to eq(2)
    end
  end
end

If I run the test :-

arup@linux-wzza:~/Ruby> rspec spec/test_spec.rb
.F

Failures:

  1) Thing using subject! returns memoized version on first invocation
     Failure/Error: expect(Thing.count).to eq(1)

       expected: 1
            got: 3

       (compared using ==)
     # ./spec/test_spec.rb:13:in `block (3 levels) in <top (required)>'

Finished in 0.00173 seconds (files took 0.13718 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/test_spec.rb:11 # Thing using subject! returns memoized 
version on first invocation


Now my question is how @count incremented by 3, shouldn't it be by 2 ?

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rspec/d1e28613-e751-4b1a-9a9d-79a2ff02e419%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to