1

I've got a class where in initializer I need to call instance variable from parsed params:

class PrintResults
  include SortResults

  attr_accessor :views_hash

  def initialize(parser)
    @parser = parser
    @views_hash = parser.page_views
  end

I want to test attributes accessors, I tried something below:

RSpec.describe PrintResults do
  subject { described_class.new(views_hash) }

  describe 'attributes accessors' do
    let(:accessors) { double(page_views: { '/that_70s_show' => ['111.111.111.111'] }) }

    it 'should have views hash' do
      subject.views_hash = accessors
      expect(subject.views_hash).to eq(['111.111.111.111'])
    end
  end

but I'm getting an error:

  1) PrintResults attributes accessors should have views hash
     Failure/Error: expect(subject.views_hash).to eq(['111.111.111.111'])

       expected: ["111.111.111.111"]
            got: #<Double (anonymous)>

       (compared using ==)

       Diff:
       @@ -1 +1 @@
       -["111.111.111.111"]
       +#<Double (anonymous)>

1 Answer 1

4

You assign your test double directly to the attribute that is returned instead of using the initialize method.

Instead of

subject { described_class.new(views_hash) }

describe 'attributes accessors' do
  let(:accessors) { double(page_views: { '/that_70s_show' => ['111.111.111.111'] }) }

  it 'should have views hash' do
    subject.views_hash = accessors
    expect(subject.views_hash).to eq(['111.111.111.111'])
  end
end

use

subject { described_class.new(parser) }

describe 'attributes accessors' do
  let(:parser) { double(page_views: { '/that_70s_show' => ['111.111.111.111'] }) }

  it 'should have views hash' do
    expect(subject.views_hash).to eq('/that_70s_show' => ['111.111.111.111'])
  end
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.