Skip to content

Instantly share code, notes, and snippets.

@billhorsman
Last active December 19, 2015 01:19
Show Gist options
  • Save billhorsman/5874742 to your computer and use it in GitHub Desktop.
Save billhorsman/5874742 to your computer and use it in GitHub Desktop.
Using let instance of instance variables in minitest.
# Using instance variables
describe "Foo" do
before do
@foo = FactoryGirl.create :foo
@bar = FactoryGirl.create :bar
end
it "sends message" do
assert @foo.msg("pow!")
# @bar is not used in this test
end
it "sends bar" do
assert @foo.msg(@bar)
end
end

# Using let
describe "Foo" do
let(:foo) { FactoryGirl.create :foo }
let(:bar) { FactoryGirl.create :bar }
it "sends message" do
assert foo.msg("pow!")
# bar is not used in this test, but it's not instantiated so all is good
end
it "sends bar" do
assert foo.msg(bar)
end
end

# Note, "let" is just a memoized method. Like the rest of Minitest it's pretty simple.
# These are equivalent:
let(:foo) { FactoryGirl.create :foo }
def foo do
@_memoized ||= {}
@_memoized.fetch(:foo) {|k| @_memoized[k] = FactoryGirl.create :foo }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment