Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add let! #206

Closed
wants to merge 1 commit into from

3 participants

@tamia

helpful when something has to always be done e.g. creating a user before logging in etc

@zenspider
Owner

I don't think this adds value. A plain def seems like a vastly superior solution to me.

@zenspider zenspider was assigned
@tamia

It adds feature-parity to rspec and I think it is a nice syntax-sugar, saves code and adds clarity.

def user
  @user ||= User.create!(:xxx=>1)
end

setup do
  user # call once so user is always there
end

vs

let!(:user){ User.create(:xxx => 1) }
@zenspider
Owner
  1. Feature parity with rspec has never been a goal.

  2. Having this explicitly called in setup seems like a mis-feature to me. By having a simple method, you call it when you need it and take the cost then.

  3. Plain methods are more lightweight (read: faster) than lets.

I'd be happy to help you publish this as an add-on gem and list it in my readme.

@zenspider zenspider closed this
@kstevens715

I have a gem I created to do this exact thing: https://github.com/kstevens715/minitest-bang

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 12, 2012
  1. add let!

    tamia authored
This page is out of date. Refresh to see the latest.
Showing with 39 additions and 2 deletions.
  1. +10 −1 lib/minitest/spec.rb
  2. +29 −1 test/minitest/test_minitest_spec.rb
View
11 lib/minitest/spec.rb
@@ -207,7 +207,7 @@ def self.it desc = "anonymous", &block
##
# Essentially, define an accessor for +name+ with +block+.
#
- # Why use let instead of def? I honestly don't know.
+ # basically the same as def but saves a few lines when doing 1-liners + has caching build-in
def self.let name, &block
define_method name do
@@ -217,6 +217,15 @@ def self.let name, &block
end
##
+ # same as let but is auto-executed before each example
+ #
+
+ def self.let! name, &block
+ let(name, &block)
+ before{ send(name) }
+ end
+
+ ##
# Another lazy man's accessor generator. Made even more lazy by
# setting the name for you to +subject+.
View
30 test/minitest/test_minitest_spec.rb
@@ -524,7 +524,6 @@ def _count
let :count do
$let_count += 1
- $let_count
end
it "is evaluated once per example" do
@@ -546,6 +545,35 @@ def _count
end
end
+describe MiniTest::Spec, :let! do
+ i_suck_and_my_tests_are_order_dependent!
+
+ def _count
+ $let_count2 ||= 0
+ end
+
+ let! :count do
+ $let_count2 ||= 0
+ $let_count2 += 1
+ end
+
+ it "is auto-evaluated once per example" do
+ _count.must_equal 1
+ count.must_equal 1
+ _count.must_equal 1
+ end
+
+ it "is REALLY auto-evaluated once per example" do
+ _count.must_equal 2
+
+ count.must_equal 2
+ count.must_equal 2
+
+ _count.must_equal 2
+ end
+end
+
+
describe MiniTest::Spec, :subject do
attr_reader :subject_evaluation_count
Something went wrong with that request. Please try again.