spec_writer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28afe1ceedda49fc65d0b2a73cf9bf7e58dc1694
4
+ data.tar.gz: 400281af3d659bbf225bbb9761686ded29733cd0
5
+ SHA512:
6
+ metadata.gz: 93dc4a9324b7d25bba54cf22ca40f614139f2e2e95fce2831e50742e44c791c6aaaf4714be8b76ce084fc00a8940bb5df7711917f51392af7f814d4486f981a3
7
+ data.tar.gz: e66a7ef504d34247074a8d69d8ce9a2750adf248ee0b2944e3b1600c8902829d81a37d8b3866a6a5b6d731af04de5e1e358a5d604e46267647903a11be81a7bb
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.0.0
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in spec_writer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Sunday Adefila
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # SpecWriter
2
+
3
+ We know you don't do TDD :stuck_out_tongue: but what about DDT? :smile:
4
+
5
+ We can help you DDT so you can TDD.
6
+
7
+ SpecWriter is a tool aimed to help in promoting the art of TDD, by first helping to compile the most basic tests ( supporting only model tests for now ) for your new rails application, so you can continue on the testing path as you progress in the development process.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'spec_writer', group: development
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install spec_writer
24
+
25
+ ## Dependencies
26
+ The usage of this gem depends on the use of the following tools/frameworks:
27
+
28
+ 1. Rails
29
+ 2. [Rspec-rails](https://github.com/rspec/rspec-rails) ( the testing framework for rails > 3.0 )
30
+ 3. [Factory-Girls](https://github.com/thoughtbot/factory_girl_rails)
31
+
32
+ Others that might be useful, but not entirely compulsory includes:
33
+
34
+ - Database cleaner
35
+ - Faker
36
+
37
+ ## Usage
38
+
39
+ In a rails application, set up the rspec testing, with factories in the `spec/factories` directory...
40
+
41
+ Next, to have SpecWriter compile model test for a model, simply run:
42
+
43
+ $ rails generate model_spec MODEL
44
+ generating for User model, for example, will be:
45
+
46
+ $ rails generate model_spec User
47
+ This will generate a spec file in the `spec/models/<model>_spec.rb` and populate with some good basic tests.
48
+
49
+ running `rspec` here might show some failing tests due to absence of required factory, either create those factories, or fix test as appropriate.
50
+
51
+ #### Tests covered are:
52
+ - Validation tests
53
+ - Association tests
54
+ - Graceful destroyals for associated models ( This is usually useful to identify where the `dependent: destroy` option has been omited so as to avoid the common `update or delete on table "<table>" violates foreign key constraint` error on a resource.)
55
+
56
+ ## Development
57
+
58
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
59
+
60
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
61
+
62
+ ## Contributing
63
+
64
+ Bug reports and pull requests are welcome on GitHub at https://github.com/SundayAdefila/spec_writer.
65
+
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
70
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "spec_writer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate model_spec Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,9 @@
1
+ require 'rails/generators/base'
2
+
3
+ class ModelSpecGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_spec_file
7
+ template "model_spec.rb", "spec/models/#{file_name.singularize.downcase}_spec.rb"
8
+ end
9
+ end
@@ -0,0 +1,108 @@
1
+ <% klass = class_name.camelize.singularize.constantize %>
2
+
3
+ <% validations = klass.validators.group_by{|a| a.kind} %>
4
+ <% validators = validations.keys %>
5
+
6
+ <% owner_klasses = klass.reflect_on_all_associations(:belongs_to).map{|asc| asc.name.to_s} %>
7
+ <% supa_klasses = klass.reflections.collect{|a, b| {class_name: b.class_name, name: b.name.to_s, options: b.options} if b.macro==:has_many}.compact %>
8
+ <% direct_supa_klasses = klass.reflections.collect{|a, b| {class_name: b.class_name, name: b.name.to_s, options: b.options} if b.macro==:has_one}.compact %>
9
+
10
+ # TODO: maybe add one for :has_and_belongs_to_many?
11
+ # TODO: Implement insertions for existing files... why??
12
+
13
+ require 'rails_helper'
14
+
15
+ RSpec.describe <%= klass %>, type: :model do
16
+ it "should have a valid factory" do
17
+ <%= file_name %> = FactoryGirl.build(:<%= file_name %>)
18
+ expect(<%= file_name %>).to be_valid
19
+ end
20
+
21
+ describe "Validators" do
22
+
23
+ <% if validators.include? :presence %>
24
+ <% validations[:presence][0].attributes.each do |attr| %>
25
+ it "should ensure the presence of <%= attr %>" do
26
+ <%= file_name %> = FactoryGirl.build(:<%= file_name %>, <%= attr %>: nil)
27
+ expect(<%= file_name %>).not_to be_valid
28
+ expect(<%= file_name %>.errors[:<%= attr %>]).to be_present
29
+ end
30
+ <% end %>
31
+ <% end %>
32
+
33
+ <% if validators.include? :uniqueness %>
34
+ <% validations[:uniqueness].each do |v| %>
35
+ <% scope = v.options[:scope] %>
36
+ <% v.attributes.each do |attr| %>
37
+ it "should ensure the uniqueness of <%= attr %>" do
38
+ <%= file_name %> = FactoryGirl.create(:<%= file_name %>)
39
+ <% if scope %>
40
+ new_<%= file_name %> = FactoryGirl.build(:<%= file_name %>, <%= attr %>: <%= file_name %>.<%= attr %>, <%= scope %>: <%= file_name %>.<%= scope %>)
41
+ <% else %>
42
+ new_<%= file_name %> = FactoryGirl.build(:<%= file_name %>, <%= attr %>: <%= file_name %>.<%= attr %>)
43
+ <% end %>
44
+ expect(new_<%= file_name %>).not_to be_valid
45
+ expect(new_<%= file_name %>.errors[:<%= attr %>]).to be_present
46
+ end
47
+ <% end %>
48
+ <% end %>
49
+ <% end %>
50
+ end
51
+
52
+ <% unless owner_klasses.empty? and supa_klasses.empty? %>
53
+
54
+ describe "Associations" do
55
+ <% owner_klasses.each do |owner| %>
56
+ it "should belong to a <%= owner %>" do
57
+ <%= owner %> = FactoryGirl.create(:<%= owner %>)
58
+ <%= file_name %> = FactoryGirl.build(:<%= file_name %>, <%= owner %>: <%= owner %>)
59
+ expect(<%= file_name %>.<%= owner %>).to eq <%= owner %>
60
+ end
61
+ <% end %>
62
+
63
+ <% supa_klasses.each do |child| %>
64
+ it "should allow multiple <%= child[:name] %>" do
65
+ <%= file_name %> = FactoryGirl.create(:<%= file_name %>)
66
+
67
+ 3.times.each do |n|
68
+ <%= child[:name].singularize %> = FactoryGirl.create(:<%= child[:class_name].underscore %>)
69
+ <%= file_name %>.<%= child[:name] %> << <%= child[:name].singularize %>
70
+ <%= "#{file_name}_#{child[:name]}" %> = <%= file_name %>.<%= child[:name] %>
71
+ expect(<%= "#{file_name}_#{child[:name]}" %>.count).to eq n.next
72
+ expect(<%= "#{file_name}_#{child[:name]}" %>).to include <%= child[:name].singularize %>
73
+ end
74
+ end
75
+
76
+ <% end %>
77
+ end
78
+ <% end %>
79
+
80
+ <% unless direct_supa_klasses.empty? and supa_klasses.empty? %>
81
+ describe "Graceful Destroyal" do
82
+ <% direct_supa_klasses.each do |d_s_k| %>
83
+ <% next if d_s_k[:options].include? :through %>
84
+ it "should destroy the associated <%= d_s_k[:name] %> when deleted" do
85
+ <%= file_name %> = FactoryGirl.create(:<%= file_name %>)
86
+ <%= d_s_k[:name] %> = FactoryGirl.create(:<%= d_s_k[:name] %>)
87
+ <%= file_name %>.<%= d_s_k[:name].pluralize %> << <%= d_s_k[:name] %>
88
+
89
+ expect{ <%= file_name %>.destroy }.to change(<%= d_s_k[:class_name].constantize %>, :count).by -1
90
+ end
91
+ <% end %>
92
+
93
+ <% supa_klasses.each do |child| %>
94
+ <% next if child[:options].include? :through %>
95
+ it "should destroy the associated <%= child[:name] %> when deleted" do
96
+ <%= file_name %> = FactoryGirl.create(:<%= file_name %>)
97
+ <%= file_name %>.<%= child[:name] %>.create(FactoryGirl.attributes_for(:<%= child[:class_name].underscore %>))
98
+
99
+ expect{ <%= file_name %>.destroy }.to change(<%= child[:class_name].constantize %>, :count).by -1
100
+ end
101
+ <% end %>
102
+ end
103
+ <% end %>
104
+
105
+ describe "Behavior" do
106
+ pending "add some examples to #{__FILE__} for behaviours or delete the 'Behaviour' test there."
107
+ end
108
+ end
@@ -0,0 +1,5 @@
1
+ require "spec_writer/version"
2
+
3
+ module SpecWriter
4
+ # Your code goes here...
5
+ end
@@ -0,0 +1,3 @@
1
+ module SpecWriter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'spec_writer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "spec_writer"
8
+ spec.version = SpecWriter::VERSION
9
+ spec.authors = ["Sunday Adefila"]
10
+ spec.email = ["adefilaedward@gmail.com"]
11
+
12
+ spec.summary = %q{Automate Rspec test writing for common MVC specs}
13
+ spec.description = %q{Rspec model tests only comes with one line: 'pending "add some examples to (or delete) #{__FILE__}"'. This gem seeks to populate the model spec with basic tests like validations, relationships, etc..., and then some.}
14
+ spec.homepage = "https://github.com/SundayAdefila/spec_writer"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.12"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "minitest", "~> 5.0"
33
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spec_writer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sunday Adefila
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: 'Rspec model tests only comes with one line: ''pending "add some examples
56
+ to (or delete) #{__FILE__}"''. This gem seeks to populate the model spec with basic
57
+ tests like validations, relationships, etc..., and then some.'
58
+ email:
59
+ - adefilaedward@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - lib/generators/model_spec/USAGE
73
+ - lib/generators/model_spec/model_spec_generator.rb
74
+ - lib/generators/model_spec/templates/model_spec.rb
75
+ - lib/spec_writer.rb
76
+ - lib/spec_writer/version.rb
77
+ - spec_writer.gemspec
78
+ homepage: https://github.com/SundayAdefila/spec_writer
79
+ licenses:
80
+ - MIT
81
+ metadata:
82
+ allowed_push_host: https://rubygems.org
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.5.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Automate Rspec test writing for common MVC specs
103
+ test_files: []