1

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

4

0 回答 0