Last active
December 19, 2015 01:19
-
-
Save billhorsman/5874742 to your computer and use it in GitHub Desktop.
Using let instance of instance variables in minitest.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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