0

I have simple controller:

class CreditLogsController < BaseController

  def show
    @account = Account.find_by_email(Base64.urlsafe_decode64(params[:id]))
  end
end

and here is the spec for it:

require 'rails_helper'

describe CreditLogsController, type: :controller do

  describe 'GET #show' do
    it 'responds with 200' do
      create(:account)

      get :show, params: {id: Base64.urlsafe_encode64('[email protected]')}, format: :html
      puts "############# #{controller.instance_variable_get(:account)}"
      expect(assigns(:account)).to eql('[email protected]')
    end
  end

end

The problems is that account in spec is always nil, in coverage file code from controller which assigns value to @account is showed as not covered and controller.instance_variable_get(:account) raises an error:

`account' is not allowed as an instance variable name.

I have similar code in other spec and is working ok, so what am I doing wrong?

2
  • Do you use FactoryGirl to create account? you are comparing an object @account with a string '[email protected]'? It should be expect(assigns(:account).email).to eql('[email protected]') Commented Dec 22, 2016 at 16:30
  • seems that the code pointer hasn't reach the show action method Commented Feb 20, 2018 at 19:17

1 Answer 1

1

As the error indicates, that's the wrong name for an instance variable. They must start with @, and so do the symbols and strings describing them.

You must use:

controller.instance_variable_get(:@account)

or

controller.instance_variable_get('@account')

The rest of your test (expect(assigns(:account))...) will not be reached, as instance_variable_get(:account) will raise a NameError exception.

Sign up to request clarification or add additional context in comments.

3 Comments

if I use controller.instance_variable_get(:@account) I have the same problem, value is nil
@maki So your find_by_email isn't working, and I can't see any reason why it would. Do some debugging, drop a binding.pry in there. We can't fix everything for you.
you are not right, the problem is not in find_by_email, for example if I just assign some value @account = "test" it's still nil in spec

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.