Refactoring - Null Object: Part One

This topic is for the [Null Object - Part One][] exercise in the [Refactoring][] trail. Post any questions, corrections, or pointers you have to share with other Upcase subscribers.
[Null Object - Part One]: https://exercises.upcase.com/exercises/null-object-part-one
[Refactoring]: https://thoughtbot.com/upcase/refactoring

Rake tests did not work upon cloning:

I found I needed to specify the rspec version in the Gemfile, and then a test failed.

Failures:

  1. User#has_mentoring? returns false without a subscription
    Failure/Error: expect(user.has_mentoring?).to be_false
    expected to respond to false? or perhaps you meant be false or be_falsey

    ./spec/user_spec.rb:36:in `block (3 levels) in <top (required)>’

This was before any changes to the file. If anyone has any similar issues, let me know.

Which RSpec version did you specify?

Sorry, Andy. That would have been helpful information.

The version was not specified in the original gemfile, but it was 2.14.4. I updated to 3.3.0. Then I received the following deprecations and failures when I called rake.

Failures:

1) User#has_mentoring? returns false without a subscription
Failure/Error: expect(user.has_mentoring?).to be_false
expected to respond to false? or perhaps you meant be false or be_falsey
# ./spec/user_spec.rb:36:in `block (3 levels) in <top (required)>’

Deprecation Warnings:

Using stub from rspec-mocks’ old :should syntax without explicitly enabling the syntax is deprecated. Use the new :expect syntax or explicitly enable :should instead. Called from /Users/gwendolyn/Code/upcase/null-object-part-one/spec/subscription_spec.rb:26:in `block (3 levels) in <top (required)>’.

If you need more of the backtrace for any of these deprecations to
identify where to make the necessary changes, you can configure
config.raise_errors_for_deprecations!, and it will turn the
deprecation warnings into errors, giving you the full backtrace.

1 deprecation warning total

Finished in 0.17634 seconds (files took 2.96 seconds to load)
9 examples, 1 failure

Failed examples:

rspec ./spec/user_spec.rb:33 # User#has_mentoring? returns false without a subscription

Is the sha 81e4aea24c8576fe7794e5343907849bf9eff811?

I needed to update RSpec

bundle update rspec

and then in spec/user_spec.rb I changed the test ‘returns false without a subscription’ that was complaining about to_be_false to to_be_falsy. After that I was green again.

1 Like

Thanks for this!

1 Like

on respc 3.4.0 i also needed to update the following, in addition to ed’s falsy change:

On line 8 of user_spec.rb:
subscription.stub(:charge)
to
allow(subscription).to receive(:charge)

And then on line 26 subsciption_spec.rb:
credit_card.stub(:charge)
to
allow(credit_card).to receive(:charge)

I’m having issues with failing tests for a solution I’d think would work. I keep getting “stack too deep” errors. Here’s my refactored User class. It seems like the subscription override is making them go haywire. Any thoughts?

class User
  include ActiveModel::Model
  attr_accessor :credit_card, :subscription

  def subscription
    subscription || NoSubscription.new
  end

  def charge
    subscription.charge(credit_card)
  end

  def has_mentoring?
    subscription.mentoring
  end

  def price
    subscription.price
  end
end

In the subscription method, reference it locally by using the instance version

@subscription || NoSubscription.new

Current version is calling itself, causing an infinite loop.

Thank you! That was exactly the problem. Silly mistake on my part :grin:

1 Like