Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting NameError: `format' is not allowed as an instance variable name when testing instance variable with rspec

Tags:

ruby

rspec

I have the following test:

let(:client) { Descat::Client.new }

describe 'poblacio' do
  it 'should set format correctly' do
    client.poblacio('v1','json','dades')
    expect (client.instance_variable_get(:format)).to eq('json')
  end
end

And I have the following code that is being tested:

module Descat
  class Client
    BASE_URL = 'http://api.idescat.cat/'

    def initialize(attributes = {})
      attributes.each do |attr, value|
        self.send("#{attr}=", value)
      end
    end

    def poblacio(version, format, operation, *args)
      @format = format
    end
  end
end

WHen running the tests I keep getting '

Failure/Error: expect (client.instance_variable_get(:format)).to eq('json')
     NameError:

But, changing the name doesnt help.

'

like image 898
Hommer Smith Avatar asked Nov 11 '13 03:11

Hommer Smith


1 Answers

To use instance_variable_get you have to provide the name starting with "@". In your case:

client.instance_variable_get('@format')
like image 139
mechanicalfish Avatar answered Sep 21 '22 14:09

mechanicalfish