问题
Was writing some tests and ran into an issue with using let
to define some variables.
I spent some time producing a minimal failing example:
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
This test fails
nested 1 before {:message=>{:to=>"recipient1", :from=>"sender1"}}
nested 2 before {:message=>{:to=>"recipient1", :from=>"sender1"}}
expected: nil
got: "recipient1"
However, commenting out line #8 of the example causes the test to pass
#attachments_dir
Why would the top level let
interfere with the nested context let
s? This feels like a RSpec bug to me, but I figured I'd ask here before posting to their issues list.
来源:https://stackoverflow.com/questions/12906268/rspec-rails-and-using-let-leading-to-variable-conflicts