Skip to content

Instantly share code, notes, and snippets.

@phorsuedzie
Last active December 11, 2015 11:28
Show Gist options
  • Save phorsuedzie/4593486 to your computer and use it in GitHub Desktop.
Save phorsuedzie/4593486 to your computer and use it in GitHub Desktop.
# Do you expect this spec to fail? If not - give it a try:
# mkdir -p spec &&
# curl # "paste in the raw url here" > spec/bad_let_spec.rb &&
# rspec spec/bad_let_spec.rb
# A hint: this spec can be turned into green
# - without changing any code in the context sections
# - without changing any let (in case you have the same bad feeling about a `let` being
# initialized from an `@variable` from a `before(:all)`)
# "Solution" below
describe "..." do
before(:all) {
@a = "before all"
a
}
let(:a) {@a}
context "context 1" do
let(:b) {"context 1"}
it "..." do
b.should == "context 1"
a.should == "before all"
end
end
context "context 2" do
let(:b) {"context 2"}
it "..." do
b.should == "context 2"
a.should == "before all"
end
end
end
# This would already turn the spec into green:
#
#
#
#
#
#
#
#
# < @a = "before all"
# > a = @a = "before all"
# This will defer the let(:a) to be executed not before the "example run". And somehow, the example let will then not leak.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment