21

次のテストがあります。

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

そして、テスト中の次のコードがあります。

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

テストを実行すると、「

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

ただし、名前の変更は役に立ちません。

'

4

1 に答える 1

47

使用instance_variable_getするには、 で始まる名前を指定する必要があります"@"。あなたの場合:

client.instance_variable_get('@format')
于 2013-11-11T04:25:47.930 に答える