我有以下测试:
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:
但是,更改名称没有帮助。
'
最佳答案
使用instance_variable_get您必须提供以 "@" 开头的名称。在你的情况下:
client.instance_variable_get('@format')
关于ruby - 获取名称错误 : `format' is not allowed as an instance variable name when testing instance variable with rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19898574/