rspec-let_as 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f97eedd83f7c6a9241c9f3759a1807d9ff7b3edfd170e7a4320a4a1d292e91d7
4
+ data.tar.gz: 6c53d48ead218f4876595920e43802749c769ca542aadfde28fc454cb0368beb
5
+ SHA512:
6
+ metadata.gz: 1c83622024411b5fa628cf5d7aac028c3633ab3e3c804823952ad9b6d1122a9ddf1d4c19fa5030bd8e4eb65e503cec9c16d7869ee1740f937bbcbf1de7f09694
7
+ data.tar.gz: 92dcf8380c701f334bb30c78a36239814e9903dd62e3b10e50ece457b5ed838b15ddd929b5bd8ac64a77fe630f09ad7c8c2520277679f8c8f084d163b0b55c33
data/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # RSpec::LetAs
2
+
3
+ Right assignment extension for RSpec
4
+
5
+ ``` ruby
6
+ using RSpec::LetAs
7
+
8
+ RSpec.describe 'answer' do
9
+ before do
10
+ 42.let_as :answer
11
+ end
12
+
13
+ example do
14
+ expect(answer).to eq(42)
15
+ end
16
+ end
17
+ ```
18
+
19
+ ## Motivation
20
+
21
+ When testing Rails applications with RSpec, I am often faced with the need to arrange large object graph. And since I also need to be able to access specific nodes in the graph, I have to create edges one by one.
22
+
23
+ ``` ruby
24
+ RSpec.describe MyApp do
25
+ let(:user) { create(:user) }
26
+ let(:post) { create(:post, user: user) }
27
+ let(:comment_1) { create(:comment, post: post) }
28
+ let(:comment_2) { create(:comment, post: post) }
29
+
30
+ # ...
31
+ end
32
+ ```
33
+
34
+ It is often difficult to imagine the resulting graph from such code. So RSpec::LetAs makes it possible to export the necessary references while building the graph hierarchically.
35
+
36
+ ``` ruby
37
+ using RSpec::LetAs
38
+
39
+ RSpec.describe MyApp do
40
+ before do
41
+ create(:user, posts: [
42
+ build(:post, comments: [
43
+ build(:comment).let_as(:comment_1),
44
+ build(:comment).let_as(:comment_2)
45
+ ]).let_as(:post)
46
+ ]).let_as(:user)
47
+ end
48
+
49
+ # ...
50
+ end
51
+ ```
52
+
53
+ ## Usage
54
+
55
+ It is implemented as a refinement and should be passed as an argument to `Module#using`.
56
+
57
+ ``` ruby
58
+ using RSpec::LetAs
59
+ ```
60
+
61
+ Then `Object#let_as` will be defined in the scope of the file or class. It can be used in example group (`describe` / `context`) or example (`example` / `it` / `before` / `let` / etc.).
62
+
63
+ ``` ruby
64
+ using RSpec::LetAs
65
+
66
+ RSpec.describe 'let_as' do
67
+ 1.let_as :one
68
+
69
+ before do
70
+ 2.let_as :two
71
+ end
72
+
73
+ let(:three) { 3.let_as(:three_2) }
74
+
75
+ example do
76
+ four_2 = 4.let_as(:four)
77
+
78
+ expect(one).to eq(1)
79
+ expect(two).to eq(2)
80
+ expect(three).to eq(3)
81
+ expect(three_2).to eq(3)
82
+ expect(four).to eq(4)
83
+ expect(four_2).to eq(4)
84
+ end
85
+ end
86
+ ```
87
+
88
+ ## Installation
89
+
90
+ Add this line to your application's Gemfile:
91
+
92
+ ``` ruby
93
+ gem 'rspec-let_as'
94
+ ```
95
+
96
+ And then execute:
97
+
98
+ $ bundle install
99
+
100
+ Or install it yourself as:
101
+
102
+ $ gem install rspec-let_as
103
+
104
+ ## License
105
+
106
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,5 @@
1
+ module RSpec
2
+ module LetAs
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,36 @@
1
+ require 'binding_of_caller'
2
+ require 'rspec/core/example_group'
3
+
4
+ require_relative 'let_as/version'
5
+
6
+ module RSpec
7
+ module LetAs
8
+ class Error < StandardError; end
9
+
10
+ refine Object do
11
+ def let_as(name)
12
+ example_group = binding.callers.lazy.map(&:receiver).find {|receiver|
13
+ receiver.is_a?(Class) && receiver.ancestors.include?(RSpec::Core::ExampleGroup)
14
+ }
15
+
16
+ raise Error, <<~EOS unless example_group
17
+ must be called from an example group or from within:
18
+
19
+ RSpec.describe 'answer' do
20
+ before do
21
+ 42.let_as :answer
22
+ end
23
+
24
+ example do
25
+ expect(answer).to eq(42)
26
+ end
27
+ end
28
+ EOS
29
+
30
+ tap {|val|
31
+ example_group.let(name) { val }
32
+ }
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1 @@
1
+ require 'rspec/let_as'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-let_as
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keita Urashima
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: binding_of_caller
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-core
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email:
43
+ - ursm@ursm.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - README.md
49
+ - lib/rspec-let_as.rb
50
+ - lib/rspec/let_as.rb
51
+ - lib/rspec/let_as/version.rb
52
+ homepage: https://github.com/ursm/rspec-let_as
53
+ licenses:
54
+ - MIT
55
+ metadata:
56
+ homepage_uri: https://github.com/ursm/rspec-let_as
57
+ source_code_uri: https://github.com/ursm/rspec-let_as.git
58
+ changelog_uri: https://github.com/ursm/rspec-let_as/releases
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.2.31
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Right assignment extension for RSpec
78
+ test_files: []