我在rspec中有一个describe块,它看起来像这样:
this.layoutscroll...
我期待看到这个输出:
describe 'Trying to get this to work' do
object = Class.new('Old Value')
it "object.value should return #{object.value}" do
expect(object.value).to eq 'Old Value'
end
describe 'Try a before hook' do
before {
object.update_value 'New Value'
}
it "object.value should return #{object.value}" do
expect(object.value).to eq 'New Value'
end
end
end
但这是我实际看到的:
Trying to get this to work
object.value should return Old Value
Try a before hook
object.value should return New Value
但测试通过,所以我知道在我的第二个例子中object.value =='新值'。
Rspec如何处理这些块,为什么我在第二个示例的描述中没有看到更新的对象值?
答案 0 :(得分:2)
使用let
describe 'Trying to get this to work' do
let(:object){ Class.new('Old Value') }
it "object.value should return #{object.value}" do
expect(object.value).to eq 'Old Value'
end
describe 'Try a before hook' do
let(:object){ Class.new('New Value') }
it "object.value should return #{object.value}" do
expect(object.value).to eq 'New Value'
end
end
end
此外,我认为您不应该依赖描述中的外部内容。您应该更明确地声明it
:
it "object.value should return new value"
更简单!