|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "quickdraw/rspec" |
| 4 | + |
| 5 | +Quickdraw::RSpec.describe "let" do |
| 6 | + let(:value) { "hello" } |
| 7 | + |
| 8 | + it "provides the value" do |
| 9 | + expect(value).to eq("hello") |
| 10 | + end |
| 11 | + |
| 12 | + it "memoizes the value" do |
| 13 | + expect(value).to equal(value) |
| 14 | + end |
| 15 | + |
| 16 | + describe "with a counter" do |
| 17 | + let(:counter) { @count ||= 0; @count += 1 } |
| 18 | + |
| 19 | + it "is lazy - not evaluated until called" do |
| 20 | + # counter not called yet, so @count should be nil |
| 21 | + expect(@count).to be_nil |
| 22 | + expect(counter).to eq(1) |
| 23 | + end |
| 24 | + |
| 25 | + it "memoizes across multiple calls in same test" do |
| 26 | + expect(counter).to eq(1) |
| 27 | + expect(counter).to eq(1) |
| 28 | + expect(counter).to eq(1) |
| 29 | + end |
| 30 | + end |
| 31 | + |
| 32 | + describe "inheritance" do |
| 33 | + let(:inherited) { "parent" } |
| 34 | + |
| 35 | + describe "nested" do |
| 36 | + it "inherits let from parent" do |
| 37 | + expect(inherited).to eq("parent") |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + describe "override" do |
| 42 | + let(:inherited) { "child" } |
| 43 | + |
| 44 | + it "can override parent let" do |
| 45 | + expect(inherited).to eq("child") |
| 46 | + end |
| 47 | + end |
| 48 | + end |
| 49 | + |
| 50 | + describe "referencing other lets" do |
| 51 | + let(:first) { "hello" } |
| 52 | + let(:second) { "#{first} world" } |
| 53 | + |
| 54 | + it "can reference other let values" do |
| 55 | + expect(second).to eq("hello world") |
| 56 | + end |
| 57 | + end |
| 58 | +end |
0 commit comments