いくつかのテストを書いていて、let
いくつかの変数を定義するために使用する際に問題が発生しました。
私は、最小限の失敗例を作成するのに時間を費やしました。
require "spec_helper"
describe "Some behavior" do
let(:attachments_dir) { "some_test_dir" }
before :all do
attachments_dir # Commenting out this line makes the tests pass
end
context "nested 1" do
let(:api_params) do
{ message: { to: "recipient1", from: "sender1"} }
end
before do
puts "nested 1 before " + api_params.to_s
end
it "nested 1 example 1" do
api_params[:message][:to].should == "recipient1"
end
end
context "nested 2" do
let(:api_params) do
{ message: { from: "sender2"} }
end
before do
puts "nested 2 before " + api_params.to_s
end
it "nested 2 example 1" do
api_params[:message][:to].should be_nil
end
end
end
このテストは失敗します
nested 1 before {:message=>{:to=>"recipient1", :from=>"sender1"}}
nested 2 before {:message=>{:to=>"recipient1", :from=>"sender1"}}
expected: nil
got: "recipient1"
ただし、例の8行目をコメントアウトすると、テストに合格します。
#attachments_dir
トップレベルlet
がネストされたコンテキストに干渉するのはなぜlet
ですか?これは私にはRSpecのバグのように感じますが、問題リストに投稿する前にここで質問すると思いました。