Rspec'Subject'和'let`不记块的值?

问题描述 投票:0回答:1

我了解到 letsubject 应该从他们所给的块中记下值,它会是懒惰的评估。

但是当我运行下面的规范时,我注意到打印的2个object_ids是不同的。

    describe "::build_stacks" do
        subject(:board) do
            Board.new(4,4)
        end

        it "should accept a number of stacks as an arg" do
            p board.object_id
            expect { Board::build_stacks(7) }.to_not raise_error
        end

        it "should return a 2D array containing the given number of empty stacks (subarrays of length 0)" do
            stacks_1 = Board::build_stacks(7)
            p board.object_id

我注意到打印出来的两个object_ids是不一样的. :board 会被记下来吗?

ruby rspec
1个回答
1
投票

参考这里 https:/relishapp.comrspecrspec-coredocshelper-methodslet-and-let。

价值从 let 会在同一个例子中的多次调用中被缓存,但不会跨例子。

希望能帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.