rspec-rails和使用let导致变量冲突?

时间:2012-10-16 01:18:34

标签: ruby-on-rails ruby rspec

正在编写一些测试并遇到使用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错误,但我想在发布到他们的问题列表之前我会问这里。

0 个答案:

没有答案