1
我已经搜索了一个答案,但我似乎无法弄清楚发生了什么问题。我有一个API客户端测试,看起来像下面这样:在之前的块中rspec范围的问题
module MyTests
describe '#update' do
# using a before(:all) block for setup
before(:all) do
@client1 = Client.new
@initial_payload_state = @client1.update.payload
end
context 'with a known starting payload' do
# The payload is some nasty nested json so I grab an existing one
# and then use a helper method to convert it to a full payload.
# Then I update the client with the new payload. I'm using before(:each)
# so I can get the client into this state for every test.
before(:each) do
@full_payload_state = helper_method(@initial_payload_state)
end
context 'alter_payload_1 works' do
# now that I have the payload in its full state I'd like to alter it to
# produce a certain output
before(:all) do
@new_payload_state = alter_payload_1(@full_payload_state)
end
# I now want to update the client with the altered payload and make sure
# it has the same data. The request and response bodies are formatted slightly
# differently in this case.
it 'works' do
@updated_payload_state = @client1.update(@new_payload_state)
expect(payloads_equal?(@full_payload_state, @new_payload_state).to eq true
end
end
context 'alter_payload_2 works' do
before(:all) do
@new_payload_state = alter_payload_2(@full_payload_state)
end
it 'works' do
@updated_payload_state = @client1.update(@new_payload_state)
expect(payloads_equal?(@full_payload_state, @new_payload_state).to eq true
end
end
在现实中,我为建立之前块是更长的时间,所以我认为是有意义的保持这种方式。我尝试使用before(:each)块,以便可以使用相同的已知状态来启动每个alter_payload上下文。问题是,这种设置,我得到一个无方法错误这条线:
@new_payload_state = alter_payload_1(@full_payload_state)
表明@full_payload_state为零。我确信我的范围有问题,但我不确定为什么或如何解决它。任何帮助非常感谢!