You have a typo in your Thing code:

  def self.count=(val)
    # Typo is +=, use just =
    @count += val
  end

That should just be @count = val not +=. This is causing count to increase
in size each time by 1 plus itself.



On Tue, Jul 1, 2014 at 4:25 PM, Surya <[email protected]> wrote:

> I think subject() defines the value which will be returned by the subject
> method:
> https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/subject/explicit-subject
>
> context "using subject!" do
>     subject { Thing.new }
>
>     it "returns 1" do
>       expect(subject.class.count).to eq(1)
>     end
> end
>
>
>
> On Wed, Jul 2, 2014 at 1:10 AM, Arup Rakshit <[email protected]> wrote:
>
>> #######
>> 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
>> <https://groups.google.com/d/msgid/rspec/d1e28613-e751-4b1a-9a9d-79a2ff02e419%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
>
> Please consider the environment before printing this email.
>
> Regards,
> Surya
>
> --
> 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/CAHGOqz489cB6o00nC7NKfa5%3DoMKXwirRPDpffM6%2BsbsFip2-xg%40mail.gmail.com
> <https://groups.google.com/d/msgid/rspec/CAHGOqz489cB6o00nC7NKfa5%3DoMKXwirRPDpffM6%2BsbsFip2-xg%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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/CAKCESdiuNRV8JAFu-GpkSgJwktR9efOvLEqqm9hH01DmL_65-Q%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to