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的具体细节。 –