rspec-lets 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ rspec-lets
2
+ ====================
3
+
4
+ `lets` and `lets` as an alternate syntax for `let` and `let!` that allow
5
+ you to define multiple variables within one call, for cleaner
6
+ definitions of test variables. As a bonus, you also get `help` to define
7
+ helper methods in the same declarative way!
8
+
9
+ ## Usage
10
+
11
+ Add this to your Rails Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rspec-lets'
15
+ ```
16
+
17
+ In your `spec_helper` add:
18
+
19
+ ```ruby
20
+ # At the top of the file
21
+ require 'rspec_lets_helper'
22
+
23
+ # In the config block
24
+ config.include RSpecLetsHelper
25
+ ```
26
+
27
+ In your specs, instead of writing:
28
+
29
+ ```ruby
30
+ let(:admin) { create(:admin) }
31
+ let(:user) { create(:user) }
32
+ ```
33
+
34
+ You can now write:
35
+
36
+ ```ruby
37
+ lets(admin: -> { create(:admin),
38
+ user: -> { create(:user))
39
+ ```
40
+
41
+ Or alternatively:
42
+ ```ruby
43
+ lets([:admin, :user] =>
44
+ ->(key) { create(:key) })
45
+ ```
46
+
47
+ The same syntax applies for `lets!`.
48
+
49
+ You can also define a helper method in the same style:
50
+ ```ruby
51
+ help(foo: ->(val) { val * 2 })
52
+ ```
@@ -0,0 +1,38 @@
1
+ require 'active_support/core_ext/array/wrap'
2
+
3
+ module RSpecLetsHelper
4
+ def lets(hash)
5
+ hash.each do |keys, value|
6
+ Array.wrap(keys).each_with_index do |key, index|
7
+ define_method key do
8
+ @assignments ||= {}
9
+ if value.arity == 1
10
+ @assignments[key] ||= instance_exec(index, &value)
11
+ elsif value.arity == 2
12
+ @assignments[key] ||= instance_exec(index, key, &value)
13
+ else
14
+ @assignments[key] ||= instance_exec(&value)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ def lets!(hash)
22
+ lets(hash)
23
+
24
+ hash.each do |keys, value|
25
+ Array.wrap(keys).each do |key|
26
+ before { __send__(key) }
27
+ end
28
+ end
29
+ end
30
+
31
+ def help(hash)
32
+ hash.each do |var, value|
33
+ define_method var do |*args|
34
+ instance_exec(*args, &value)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe RSpecLetsHelper do
4
+ context 'simple lazy lets' do
5
+ lets(foo: -> { 'bar' },
6
+ hello: -> { 'world'})
7
+
8
+ it 'works as expected' do
9
+ expect(foo).to eq('bar')
10
+ expect(hello).to eq('world')
11
+ end
12
+
13
+ context 'forced initialization with lets!' do
14
+ lets!(foo: -> { $foo_was_initialized = true },
15
+ bar: -> { $bar_was_initialized = true})
16
+
17
+ it 'works as expected' do
18
+ expect($foo_was_initialized).to be_true
19
+ expect($bar_was_initialized).to be_true
20
+ end
21
+ end
22
+
23
+ context 'array keys' do
24
+ lets([:a, :b, :c] => -> { 'hello world' })
25
+ lets([:d, :e, :f] => ->(index, key) { [index, key] })
26
+
27
+ it 'initializes each of the keys in the array' do
28
+ expect(a).to eq('hello world')
29
+ expect(b).to eq('hello world')
30
+ expect(c).to eq('hello world')
31
+ end
32
+
33
+ it 'can accept key and index' do
34
+ expect(d).to eq([0, :d])
35
+ expect(e).to eq([1, :e])
36
+ expect(f).to eq([2, :f])
37
+ end
38
+ end
39
+
40
+ context 'help defines helper methods' do
41
+ help(foo: ->(val) { val * 2 })
42
+
43
+ it 'defines a function' do
44
+ expect(foo(2)).to eq(4)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,8 @@
1
+ require 'pry'
2
+ require 'rspec'
3
+
4
+ require_relative '../lib/rspec_lets_helper'
5
+
6
+ RSpec.configure do |c|
7
+ c.extend RSpecLetsHelper
8
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-lets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brent Vatne
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-06-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 2.3.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 2.3.8
30
+ description: lets and lets! to declare multiple variables in one call.
31
+ email:
32
+ - brent.vatne@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/rspec_lets_helper.rb
38
+ - README.md
39
+ - spec/rspec_lets_helper_spec.rb
40
+ - spec/spec_helper.rb
41
+ homepage: https://github.com/brentvatne/rspec-lets
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.29
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: lets and lets! to declare multiple variables in one call.
65
+ test_files:
66
+ - spec/rspec_lets_helper_spec.rb
67
+ - spec/spec_helper.rb
68
+ has_rdoc: