rspec-rails and using let leading to variable conflicts?

你。 提交于 2020-02-05 06:43:03

问题


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 lets? 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!