title: InstanceDouble(session) (anonymous)> received unexpected message :[]= with
body: <p>I have a problem with rspec behavior. I try to write test for service where I use <code>session</code>, for read some value and overwrite this value.</p>
  
  <p>For example what I want to test</p>
  
  <pre><code>class CurrentCartService
    attr_reader :user, :session
  
    def initialize(user, session)
      @user = user
      @session = session
    end
  
    def cart_id
      { id: session[:cart_id] }
    end
  
    def assigne_cart_to_session
      session[:cart_id] = current_cart.id
    end
  
  </code></pre>
  
  <p><code>spec</code></p>
  
  <pre><code>describe CurrentCartService do
    let(:current_user) { user }
    let(:session) { double('session') }
  
    let!(:cart) { create(:cart) }
  
    subject { described_class.new current_user, session }
  
    before do
      allow(session).to receive(:[]).and_return(cart.id)
    end
  
    describe '#call' do
      context 'when user is not signed' do
        let(:user) { nil }
        it { subject.call }
      end
    end
  end
  </code></pre>
  
  <p><code>binding.pry</code></p>
  
  <pre><code>session[:cart_id]
  =&gt; 574
  </code></pre>
  
  <pre><code>session[:cart_id] = 123
  RSpec::Mocks::MockExpectationError: #&lt;InstanceDouble(session) (anonymous)&gt; received unexpected message :[]= with (:cart_id, 123)
  </code></pre>
  
  <p>How to fix this? I tried to write some <code>expect</code></p>
  
  <pre><code>expect(session).to receive(:[])
  </code></pre>
  
  <p>But it does not work, it's still the same error</p>