1
我在使用let
的RSpec測試中遇到了一個奇怪的行爲。我從來沒有遇到過使用let
的問題,所以這很奇怪。在下面的測試中,my_model
let
定義返回nil:RSpec「讓」方法錯誤地返回零
describe '.process' do
let(:my_model){ Fabricate(:my_model) }
it 'doesnt work' do
# my_model returns nil but it should be returning the fabricated model
my_model = Processor.process(my_model)
my_model.special_attribute.should == 'foo'
end
it 'works' do
my_model = Fabricate(:my_model)
# my_model is now correctly fabricated
my_model = Processor.process(my_model)
my_model.special_attribute.should == 'foo'
end
end
這究竟是爲什麼?
就是這樣,謝謝!我想我認爲它會評估從右到左,但顯然不是這樣。 – Andrew
它的確有,除了局部變量在達到該行時被定義而不是在被評估時被定義。我曾經記得爲什麼,但我不得不去VM的具體細節。 –