partial_testcase 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 +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +122 -0
- data/Rakefile +33 -0
- data/lib/partial_testcase/base.rb +77 -0
- data/lib/partial_testcase/version.rb +3 -0
- data/lib/partial_testcase.rb +5 -0
- data/lib/tasks/partial_testcase_tasks.rake +4 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5b326a452a8bcbe4197962d15f048bbae6709705
|
4
|
+
data.tar.gz: 55ac96987f288f880fa8336c7d4f2b927a8c4c56
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a4171512b7f46beb7d6f08a175bf83f089aec748439d74bf96eb2c067e722914444758bc57d0a01ccace187120e88f277c9c42883632c983c54cf7e3d7e6b896
|
7
|
+
data.tar.gz: b6ef53eb3d301a474c9b08ecb684ab0e631004fbf69a196e8af55b4ec59866691abe6397de37ba208bf7db91d446b40fb1b5e4f98ff6e9979c3f25fca9769669
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Julien Meichelbeck
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
# PartialTestcase
|
2
|
+
PartialTestcase is a gem providing unit tests for your partials.
|
3
|
+
|
4
|
+

|
5
|
+
|
6
|
+
## Why?
|
7
|
+
* Integration tests are slow.
|
8
|
+
* Controller tests are deprecated in favor of integration tests.
|
9
|
+
* If well written, a partial can be seen as just a simple method returning HTML. That sounds like something that can be easily tested.
|
10
|
+
* Partials can become messy, even bloated. With simple units test, it is possible to test all the variants of your view layer. How many times did you break something because your forgot to test a branch of your template?
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'partial_testcase'
|
17
|
+
```
|
18
|
+
|
19
|
+
You're ready to go!
|
20
|
+
|
21
|
+
## Simple example
|
22
|
+
Let's test the following partial:
|
23
|
+
|
24
|
+
```erb
|
25
|
+
<div>
|
26
|
+
<span class="username"><%= user.name %></span>
|
27
|
+
</div>
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
class SampleTest < PartialTestcase::Base
|
32
|
+
setup do
|
33
|
+
@user = User.new('Clark')
|
34
|
+
end
|
35
|
+
|
36
|
+
test 'just an example' do
|
37
|
+
html = render_partial('sample/user', user: @user)
|
38
|
+
|
39
|
+
# Use the same selectors as in your Controller tests
|
40
|
+
assert_select 'span.username', text: 'Clark'
|
41
|
+
|
42
|
+
# Or directly test the html
|
43
|
+
expected_html = <<~HTML
|
44
|
+
<div>
|
45
|
+
<span class="username">Clark</span>
|
46
|
+
</div>
|
47
|
+
HTML
|
48
|
+
assert_equal expected_html, html
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
## Documentation
|
55
|
+
### How to specify the partial path?
|
56
|
+
You can just specify the partial at each render:
|
57
|
+
```ruby
|
58
|
+
render_partial('sample/users', foo: 'bar')
|
59
|
+
```
|
60
|
+
|
61
|
+
Otherwise, if you test the same partial every time, use the `partial_path` method:
|
62
|
+
```ruby
|
63
|
+
class SampleTest < PartialTestcase::Base
|
64
|
+
partial_path 'sample/users'
|
65
|
+
|
66
|
+
test 'sample test' do
|
67
|
+
render_partial(foo: 'bar')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### How to declare instance variables?
|
73
|
+
Use `assign` before rendering the partial.
|
74
|
+
```ruby
|
75
|
+
assign(user: @user) # This will make @user available in the partial
|
76
|
+
render_partial
|
77
|
+
```
|
78
|
+
|
79
|
+
### How to include helpers and module?
|
80
|
+
Use `with_module`:
|
81
|
+
```ruby
|
82
|
+
class SampleTest < PartialTestcase::Base
|
83
|
+
with_module UsersHelper
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
### How to stub methods?
|
88
|
+
If you want to add specific methods for one test:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
test 'sample test' do
|
92
|
+
render_partial('sample/user') do
|
93
|
+
def format_address
|
94
|
+
# ...
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
If you need the same methods for every test, use the `with_helpers` method:
|
101
|
+
```ruby
|
102
|
+
class SampleTest < PartialTestcase::Base
|
103
|
+
with_helpers do
|
104
|
+
def country
|
105
|
+
:us
|
106
|
+
en
|
107
|
+
|
108
|
+
def format_address(address, city)
|
109
|
+
# ...
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
## Contributing
|
116
|
+
|
117
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/julien-meichelbeck/partial_testcase.
|
118
|
+
|
119
|
+
|
120
|
+
## License
|
121
|
+
|
122
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
require 'rdoc/task'
|
8
|
+
|
9
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
10
|
+
rdoc.rdoc_dir = 'rdoc'
|
11
|
+
rdoc.title = 'PartialTestcase'
|
12
|
+
rdoc.options << '--line-numbers'
|
13
|
+
rdoc.rdoc_files.include('README.md')
|
14
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
require 'bundler/gem_tasks'
|
23
|
+
|
24
|
+
require 'rake/testtask'
|
25
|
+
|
26
|
+
Rake::TestTask.new(:test) do |t|
|
27
|
+
t.libs << 'test'
|
28
|
+
t.pattern = 'test/**/*_test.rb'
|
29
|
+
t.verbose = false
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
task default: :test
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
module PartialTestcase
|
3
|
+
class Base < ::ActionView::TestCase
|
4
|
+
include Rails::Dom::Testing::Assertions
|
5
|
+
|
6
|
+
attr_reader :html_body
|
7
|
+
class_attribute :partial, :helpers_context, :modules
|
8
|
+
self.modules = []
|
9
|
+
|
10
|
+
def setup
|
11
|
+
super
|
12
|
+
setup_view
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.partial_path(partial)
|
16
|
+
self.partial = partial
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.with_helpers(&block)
|
20
|
+
self.helpers_context = block
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.with_module(mod)
|
24
|
+
modules << mod
|
25
|
+
end
|
26
|
+
|
27
|
+
def assign(**assigns)
|
28
|
+
@_assigns.merge!(assigns)
|
29
|
+
end
|
30
|
+
|
31
|
+
protected
|
32
|
+
|
33
|
+
def setup_view
|
34
|
+
@html_body = nil
|
35
|
+
@_assigns = {}
|
36
|
+
|
37
|
+
@_action_view_class =
|
38
|
+
Class.new(::ActionView::Base) do
|
39
|
+
def view_cache_dependencies; end
|
40
|
+
|
41
|
+
def combined_fragment_cache_key(key)
|
42
|
+
[:views, key]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def document_root_element
|
48
|
+
Nokogiri::XML(@html_body.to_s)
|
49
|
+
end
|
50
|
+
|
51
|
+
def render_partial(*args, &block)
|
52
|
+
@view = @_action_view_class.new(ApplicationController.view_paths, @_assigns)
|
53
|
+
|
54
|
+
add_to_context(self.class.helpers_context)
|
55
|
+
add_to_context(block)
|
56
|
+
self.class.modules.each do |mod|
|
57
|
+
@_action_view_class.include(mod)
|
58
|
+
end
|
59
|
+
|
60
|
+
options = args.extract_options!
|
61
|
+
partial_path = args[0] || self.class.partial
|
62
|
+
|
63
|
+
if partial_path.nil?
|
64
|
+
raise "You must specify the path of the partial you are testing. Call the class method 'partial_path'"
|
65
|
+
end
|
66
|
+
|
67
|
+
@html_body = @view.render(partial: partial_path, locals: options)
|
68
|
+
end
|
69
|
+
|
70
|
+
def add_to_context(block)
|
71
|
+
return if block.nil?
|
72
|
+
mod = Module.new
|
73
|
+
mod.class_eval(&block)
|
74
|
+
@_action_view_class.include(mod)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: partial_testcase
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julien Meichelbeck
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: PartialTestcase is a gem providing unit tests for your partials.
|
56
|
+
email:
|
57
|
+
- julien.meichelbeck@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- MIT-LICENSE
|
63
|
+
- README.md
|
64
|
+
- Rakefile
|
65
|
+
- lib/partial_testcase.rb
|
66
|
+
- lib/partial_testcase/base.rb
|
67
|
+
- lib/partial_testcase/version.rb
|
68
|
+
- lib/tasks/partial_testcase_tasks.rake
|
69
|
+
homepage: https://github.com/julien-meichelbeck/partial_testcase
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.5.1
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: Unit test your view layer
|
93
|
+
test_files: []
|