Skip to content

Commit

Permalink
Increase rails integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vsppedro committed Aug 29, 2020
1 parent 4e84191 commit f687de5
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 56 deletions.
106 changes: 50 additions & 56 deletions spec/acceptance/rails_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,12 @@
describe 'shoulda-matchers integrates with Rails' do
before do
create_rails_application
write_files_in_rails_application

write_file 'db/migrate/1_create_users.rb', <<-FILE
class CreateUsers < #{migration_class_name}
def self.up
create_table :users do |t|
t.string :name
end
end
end
FILE
# TODO: ActionController matchers
# configure_routes_with_single_wildcard_route

run_rake_tasks!('db:drop', 'db:create', 'db:migrate')

write_file 'app/models/user.rb', <<-FILE
class User < ActiveRecord::Base
validates_presence_of :name
end
FILE

write_file 'app/controllers/examples_controller.rb', <<-FILE
class ExamplesController < ApplicationController
def show
@example = 'hello'
head :ok
end
end
FILE

configure_routes_with_single_wildcard_route
end

specify 'in a project that uses the default test framework' do
Expand Down Expand Up @@ -105,52 +82,69 @@ def run_tests_for_n_unit
require 'test_helper'
class UserTest < ActiveSupport::TestCase
should validate_presence_of(:name)
# ActiveModel matchers
should allow_value('https://foo.com').for(:website_url)
should have_secure_password
should validate_absence_of(:first_name)
should validate_acceptance_of(:terms_of_service)
should validate_confirmation_of(:email)
should validate_exclusion_of(:age).
in_array(0..17)
should validate_inclusion_of(:role).
in_array(%w( admin manager ))
should validate_length_of(:password).
is_at_least(10).
on(:create)
should validate_numericality_of(:number_of_dependents).on(:create)
should validate_presence_of(:email)
# TODO: ActiveRecord matchers
end
FILE

write_file 'test/functional/examples_controller_test.rb', <<-FILE
require 'test_helper'
class ExamplesControllerTest < ActionController::TestCase
def setup
get :show
end
should respond_with(:success)
end
FILE
# TODO: ActionController matchers tests

result = run_n_unit_test_suite

expect(result).to indicate_that_tests_were_run(unit: 1, functional: 1)
expect(result).to have_output(
'User should validate that :name cannot be empty/falsy',
)
expect(result).to have_output('should respond with 200')
expect(result).to indicate_that_tests_were_run(unit: 10)
end

def run_tests_for_rspec
add_rspec_file 'spec/models/user_spec.rb', <<-FILE
describe User do
it { should validate_presence_of(:name) }
end
FILE

add_rspec_file 'spec/controllers/examples_controller_spec.rb', <<-FILE
describe ExamplesController, "show" do
before { get :show }
# ActiveModel matchers
it { should allow_value('https://foo.com').for(:website_url) }
it { should have_secure_password }
it { should validate_absence_of(:first_name) }
it { should validate_acceptance_of(:terms_of_service) }
it { should validate_confirmation_of(:email) }
it do
should validate_exclusion_of(:age).
in_array(0..17)
end
it do
should validate_inclusion_of(:role).
in_array(%w( admin manager ))
end
it do
should validate_length_of(:password).
is_at_least(10).
on(:create)
end
it do
should validate_numericality_of(:number_of_dependents).
on(:create)
end
it { should validate_presence_of(:email) }
it { should respond_with(:success) }
# TODO: ActiveRecord matchers
end
FILE

# TODO: ActionController matchers tests

result = run_rspec_suite

expect(result).to have_output('2 examples, 0 failures')
expect(result).to have_output(
'is expected to validate that :name cannot be empty/falsy',
)
expect(result).to have_output('is expected to respond with 200')
expect(result).to have_output('10 examples, 0 failures')
end
end
67 changes: 67 additions & 0 deletions spec/support/acceptance/helpers/step_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def create_rails_application
bundle.remove_gem 'debugger'
bundle.remove_gem 'byebug'
bundle.remove_gem 'web-console'
bundle.add_gem 'bcrypt'
bundle.add_gem 'pg'
end

Expand All @@ -83,6 +84,72 @@ def create_rails_application
end
end

def write_files_in_rails_application
write_file 'db/migrate/1_create_users.rb', <<-FILE
class CreateUsers < #{migration_class_name}
def self.up
create_table :categories_users do |t|
t.integer :category_id
t.integer :user_id
end
create_table :categories do |t|
end
create_table :issues do |t|
t.integer :user_id
end
create_table :users do |t|
t.integer :age
t.string :email
t.string :first_name
t.integer :number_of_dependents
t.string :gender
t.string :password_digest
t.string :role
t.string :website_url
end
add_index :users, :account_id
end
end
FILE

write_file 'app/models/category.rb', <<-FILE
class Category < ActiveRecord::Base
end
FILE

write_file 'app/models/issue.rb', <<-FILE
class Issue < ActiveRecord::Base
end
FILE

write_file 'app/models/user.rb', <<-FILE
class User < ActiveRecord::Base
# Note: All of these validations are listed in the same order as what's
# defined in the test (see below)
# ActiveModel
validates_format_of :website_url, with: URI.regexp
has_secure_password
validates_absence_of :first_name
validates_acceptance_of :terms_of_service
validates_confirmation_of :email
validates_exclusion_of :age, in: 0..17
validates_inclusion_of :role, in: %w( admin manager )
validates_length_of :password, minimum: 10, on: :create
validates_numericality_of :number_of_dependents, on: :create
validates_presence_of :email
# TODO: ActiveRecord
end
FILE

# TODO: Controller file
end

def configure_routes_with_single_wildcard_route
write_file 'config/routes.rb', <<-FILE
Rails.application.routes.draw do
Expand Down

0 comments on commit f687de5

Please sign in to comment.