Reputation: 535
I have a controller action with some instance variables which I want to test, here is my action code:
def index
@users_list = get_response('accounts/by_tag/QuickcallAdmin')['users'].to_a
@credit_admin = !@users_list.select{|h| h['email'] == current_user.email}.blank?
@accounts_emails = Account.pluck(:email)
end
and the spec code:
describe 'GET #index' do
login_user
let(:account) {create(:account)}
it 'responds with 200' do
get :index, :format => :html
controller.instance_variable_set(:@users_list, [{"id"=>2, "email"=>"[email protected]", "tenant_id"=>1,"first_name"=>"valera","last_name"=>"rotari","phone_number"=>"89439438954", "tags"=>[{"name"=>"Program", "value"=>"my_company"}, {"name"=>"ProgramAdmin", "value"=>"new_program"}, {"name"=>"ProgramAdmin", "value"=>"new_program1"}, {"name"=>"QuickcallAdmin", "value"=>"q1"}, {"name"=>"CreditAdmin", "value"=>"q1"}]}])
expect(assigns(:credit_admin)).to eql(true)
expect(assigns(:accounts_emails)).to eql(["[email protected]"])
end
end
so as you can see I try to assign some hash which I usually get from the response of the api call, and after that to see if credit_admin and account_emails vars has the right value. The problem is if I pus some puts in spec after var assignation I see my value assigned, but the test fails because credit admin variable is false and is expected as true. I got the same code and put it rails console to make a test for myself, note I use the same hash, and I have the right true value. So it seems like @users_list var is not assigned right or something like this. Also @account_email is empty array, but I have account created from factory, so it should have some value in it, seems like a common problem for both of them
Upvotes: 0
Views: 139
Reputation: 535
so I did it in another way, changed my spec code to
describe 'GET #index' do
login_user
it 'responds with 200' do
create(:account)
allow(controller).to receive(:get_response).and_return({"users" => [{"id"=>2, "email"=>"[email protected]","tenant_id"=>1,"first_name"=>"valera","last_name"=>"rotari","phone_number"=>"89439438954","tags"=>[{"name"=>"Program", "value"=>"my_company"}, {"name"=>"ProgramAdmin", "value"=>"new_program"}, {"name"=>"ProgramAdmin", "value"=>"new_program1"}, {"name"=>"QuickcallAdmin", "value"=>"q1"}, {"name"=>"CreditAdmin", "value"=>"q1"}]}]})
get :index, :format => :html
expect(assigns(:credit_admin)).to eql(true)
expect(assigns(:accounts_emails)).to eql(["[email protected]"])
end
end
Upvotes: 2