Ian Tran
Ian Tran

Reputation: 335

Rails 5 rspec controller test return nil result

Here I have this problem while writing test for this route

close_offer POST   (/:locale)/offers/:code/close(.:format)       offers#close {:locale=>/en|vi/}

This is the test that I wrote:

RSpec.describe OffersController, type: :controller do
  let(:access_token) { extract_access_token_from_vcr_cassette }

  describe 'POST #close' do
    describe 'successful response' do
      before(:each) do
        VCR.use_cassette('offers/successful_close_offer') do
          post :close, params: { code: 5742, access_token: access_token }, xhr: true
        end
      end

      it 'returns success? = true' do
        expect(assigns(:result)).not_to be_nil
      end
    end
  end
end

Somehow, the assigns(:result) always return nil. Although it works perfectly in tests in other routes. The real code also works, only the test failed. It seems that no operation has run, no vcr record, so I suspect that this line post :close, params: { code: 5742, access_token: access_token }, xhr: true is wrong. Anyone has a hint?

Upvotes: 0

Views: 728

Answers (1)

Will Nguyen
Will Nguyen

Reputation: 348

Yes, I used to face the same situation. Let's check you authentication. Somehow, you couldn't sign in so that the route is not like you expected, no result returned.

Upvotes: 1

Related Questions