user and recipient and tests

This commit is contained in:
Jared Cosulich 2017-02-25 11:00:27 -05:00
parent d70e30ec93
commit df2ea95ceb
50 changed files with 1412 additions and 37 deletions

View file

@ -0,0 +1,159 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec. Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.
RSpec.describe RecipientsController, type: :controller do
# This should return the minimal set of attributes required to create a valid
# Recipient. As you add validations to Recipient, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# RecipientsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET #index" do
it "assigns all recipients as @recipients" do
recipient = Recipient.create! valid_attributes
get :index, params: {}, session: valid_session
expect(assigns(:recipients)).to eq([recipient])
end
end
describe "GET #show" do
it "assigns the requested recipient as @recipient" do
recipient = Recipient.create! valid_attributes
get :show, params: {id: recipient.to_param}, session: valid_session
expect(assigns(:recipient)).to eq(recipient)
end
end
describe "GET #new" do
it "assigns a new recipient as @recipient" do
get :new, params: {}, session: valid_session
expect(assigns(:recipient)).to be_a_new(Recipient)
end
end
describe "GET #edit" do
it "assigns the requested recipient as @recipient" do
recipient = Recipient.create! valid_attributes
get :edit, params: {id: recipient.to_param}, session: valid_session
expect(assigns(:recipient)).to eq(recipient)
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Recipient" do
expect {
post :create, params: {recipient: valid_attributes}, session: valid_session
}.to change(Recipient, :count).by(1)
end
it "assigns a newly created recipient as @recipient" do
post :create, params: {recipient: valid_attributes}, session: valid_session
expect(assigns(:recipient)).to be_a(Recipient)
expect(assigns(:recipient)).to be_persisted
end
it "redirects to the created recipient" do
post :create, params: {recipient: valid_attributes}, session: valid_session
expect(response).to redirect_to(Recipient.last)
end
end
context "with invalid params" do
it "assigns a newly created but unsaved recipient as @recipient" do
post :create, params: {recipient: invalid_attributes}, session: valid_session
expect(assigns(:recipient)).to be_a_new(Recipient)
end
it "re-renders the 'new' template" do
post :create, params: {recipient: invalid_attributes}, session: valid_session
expect(response).to render_template("new")
end
end
end
describe "PUT #update" do
context "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested recipient" do
recipient = Recipient.create! valid_attributes
put :update, params: {id: recipient.to_param, recipient: new_attributes}, session: valid_session
recipient.reload
skip("Add assertions for updated state")
end
it "assigns the requested recipient as @recipient" do
recipient = Recipient.create! valid_attributes
put :update, params: {id: recipient.to_param, recipient: valid_attributes}, session: valid_session
expect(assigns(:recipient)).to eq(recipient)
end
it "redirects to the recipient" do
recipient = Recipient.create! valid_attributes
put :update, params: {id: recipient.to_param, recipient: valid_attributes}, session: valid_session
expect(response).to redirect_to(recipient)
end
end
context "with invalid params" do
it "assigns the recipient as @recipient" do
recipient = Recipient.create! valid_attributes
put :update, params: {id: recipient.to_param, recipient: invalid_attributes}, session: valid_session
expect(assigns(:recipient)).to eq(recipient)
end
it "re-renders the 'edit' template" do
recipient = Recipient.create! valid_attributes
put :update, params: {id: recipient.to_param, recipient: invalid_attributes}, session: valid_session
expect(response).to render_template("edit")
end
end
end
describe "DELETE #destroy" do
it "destroys the requested recipient" do
recipient = Recipient.create! valid_attributes
expect {
delete :destroy, params: {id: recipient.to_param}, session: valid_session
}.to change(Recipient, :count).by(-1)
end
it "redirects to the recipients list" do
recipient = Recipient.create! valid_attributes
delete :destroy, params: {id: recipient.to_param}, session: valid_session
expect(response).to redirect_to(recipients_url)
end
end
end

View file

@ -0,0 +1,11 @@
require 'rails_helper'
RSpec.describe WelcomeController, type: :controller do
describe "GET #index" do
it "works" do
get :index
end
end
end

57
spec/rails_helper.rb Normal file
View file

@ -0,0 +1,57 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end
# in _spec.rb will both be required and run as specs, causing the specs to be
# run twice. It is recommended that you do not name files matching this glob to
# end with _spec.rb. You can configure this pattern with the --pattern
# option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }
# Checks for pending migration and applies them before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# RSpec Rails can automatically mix in different behaviours to your tests
# based on their file location, for example enabling you to call `get` and
# `post` in specs under `spec/controllers`.
#
# You can disable this behaviour by removing the line below, and instead
# explicitly tag your specs with their type, e.g.:
#
# RSpec.describe UsersController, :type => :controller do
# # ...
# end
#
# The different available types are documented in the features, such as in
# https://relishapp.com/rspec/rspec-rails/docs
config.infer_spec_type_from_file_location!
# Filter lines from Rails gems in backtraces.
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

View file

@ -0,0 +1,39 @@
require "rails_helper"
RSpec.describe RecipientsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/recipients").to route_to("recipients#index")
end
it "routes to #new" do
expect(:get => "/recipients/new").to route_to("recipients#new")
end
it "routes to #show" do
expect(:get => "/recipients/1").to route_to("recipients#show", :id => "1")
end
it "routes to #edit" do
expect(:get => "/recipients/1/edit").to route_to("recipients#edit", :id => "1")
end
it "routes to #create" do
expect(:post => "/recipients").to route_to("recipients#create")
end
it "routes to #update via PUT" do
expect(:put => "/recipients/1").to route_to("recipients#update", :id => "1")
end
it "routes to #update via PATCH" do
expect(:patch => "/recipients/1").to route_to("recipients#update", :id => "1")
end
it "routes to #destroy" do
expect(:delete => "/recipients/1").to route_to("recipients#destroy", :id => "1")
end
end
end

99
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,99 @@
# This file was generated by the `rails generate rspec:install` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# The generated `.rspec` file contains `--require spec_helper` which will cause
# this file to always be loaded, without a need to explicitly require it in any
# files.
#
# Given that it is always loaded, you are encouraged to keep this file as
# light-weight as possible. Requiring heavyweight dependencies from this file
# will add to the boot time of your test suite on EVERY test run, even for an
# individual file that may not need all of that loaded. Instead, consider making
# a separate helper file that requires the additional dependencies and performs
# the additional setup, and require it from the spec files that actually need
# it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# rspec-expectations config goes here. You can use an alternate
# assertion/expectation library such as wrong or the stdlib/minitest
# assertions if you prefer.
config.expect_with :rspec do |expectations|
# This option will default to `true` in RSpec 4. It makes the `description`
# and `failure_message` of custom matchers include text for helper methods
# defined using `chain`, e.g.:
# be_bigger_than(2).and_smaller_than(4).description
# # => "be bigger than 2 and smaller than 4"
# ...rather than:
# # => "be bigger than 2"
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
# rspec-mocks config goes here. You can use an alternate test double
# library (such as bogus or mocha) by changing the `mock_with` option here.
config.mock_with :rspec do |mocks|
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended, and will default to
# `true` in RSpec 4.
mocks.verify_partial_doubles = true
end
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
# have no way to turn it off -- the option exists only for backwards
# compatibility in RSpec 3). It causes shared context metadata to be
# inherited by the metadata hash of host groups and examples, rather than
# triggering implicit auto-inclusion in groups with matching metadata.
config.shared_context_metadata_behavior = :apply_to_host_groups
# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# This allows you to limit a spec run to individual examples or groups
# you care about by tagging them with `:focus` metadata. When nothing
# is tagged with `:focus`, all examples get run. RSpec also provides
# aliases for `it`, `describe`, and `context` that include `:focus`
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
config.filter_run_when_matching :focus
# Allows RSpec to persist some state between runs in order to support
# the `--only-failures` and `--next-failure` CLI options. We recommend
# you configure your source control system to ignore this file.
config.example_status_persistence_file_path = "spec/examples.txt"
# Limits the available syntax to the non-monkey patched syntax that is
# recommended. For more details, see:
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
config.disable_monkey_patching!
# Many RSpec users commonly either run the entire suite or an individual
# file, and it's useful to allow more verbose output when running an
# individual spec file.
if config.files_to_run.one?
# Use the documentation formatter for detailed output,
# unless a formatter has already been configured
# (e.g. via a command-line flag).
config.default_formatter = 'doc'
end
# Print the 10 slowest examples and example groups at the
# end of the spec run, to help surface which specs are running
# particularly slow.
config.profile_examples = 10
# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = :random
# Seed global randomization in this process using the `--seed` CLI option.
# Setting this allows you to use `--seed` to deterministically reproduce
# test failures related to randomization by passing the same `--seed` value
# as the one that triggered the failure.
Kernel.srand config.seed
=end
end

View file

@ -0,0 +1,42 @@
require 'rails_helper'
RSpec.describe "recipients/edit", type: :view do
before(:each) do
@recipient = assign(:recipient, Recipient.create!(
:name => "MyString",
:phone => "MyString",
:gender => "MyString",
:race => "MyString",
:ethnicity => "MyString",
:home_language_id => 1,
:income => "MyString",
:opted_out => false,
:school_id => 1
))
end
it "renders the edit recipient form" do
render
assert_select "form[action=?][method=?]", recipient_path(@recipient), "post" do
assert_select "input#recipient_name[name=?]", "recipient[name]"
assert_select "input#recipient_phone[name=?]", "recipient[phone]"
assert_select "input#recipient_gender[name=?]", "recipient[gender]"
assert_select "input#recipient_race[name=?]", "recipient[race]"
assert_select "input#recipient_ethnicity[name=?]", "recipient[ethnicity]"
assert_select "input#recipient_home_language_id[name=?]", "recipient[home_language_id]"
assert_select "input#recipient_income[name=?]", "recipient[income]"
assert_select "input#recipient_opted_out[name=?]", "recipient[opted_out]"
assert_select "input#recipient_school_id[name=?]", "recipient[school_id]"
end
end
end

View file

@ -0,0 +1,43 @@
require 'rails_helper'
RSpec.describe "recipients/index", type: :view do
before(:each) do
assign(:recipients, [
Recipient.create!(
:name => "Name",
:phone => "Phone",
:gender => "Gender",
:race => "Race",
:ethnicity => "Ethnicity",
:home_language_id => 2,
:income => "Income",
:opted_out => false,
:school_id => 3
),
Recipient.create!(
:name => "Name",
:phone => "Phone",
:gender => "Gender",
:race => "Race",
:ethnicity => "Ethnicity",
:home_language_id => 2,
:income => "Income",
:opted_out => false,
:school_id => 3
)
])
end
it "renders a list of recipients" do
render
assert_select "tr>td", :text => "Name".to_s, :count => 2
assert_select "tr>td", :text => "Phone".to_s, :count => 2
assert_select "tr>td", :text => "Gender".to_s, :count => 2
assert_select "tr>td", :text => "Race".to_s, :count => 2
assert_select "tr>td", :text => "Ethnicity".to_s, :count => 2
assert_select "tr>td", :text => 2.to_s, :count => 2
assert_select "tr>td", :text => "Income".to_s, :count => 2
assert_select "tr>td", :text => false.to_s, :count => 2
assert_select "tr>td", :text => 3.to_s, :count => 2
end
end

View file

@ -0,0 +1,42 @@
require 'rails_helper'
RSpec.describe "recipients/new", type: :view do
before(:each) do
assign(:recipient, Recipient.new(
:name => "MyString",
:phone => "MyString",
:gender => "MyString",
:race => "MyString",
:ethnicity => "MyString",
:home_language_id => 1,
:income => "MyString",
:opted_out => false,
:school_id => 1
))
end
it "renders new recipient form" do
render
assert_select "form[action=?][method=?]", recipients_path, "post" do
assert_select "input#recipient_name[name=?]", "recipient[name]"
assert_select "input#recipient_phone[name=?]", "recipient[phone]"
assert_select "input#recipient_gender[name=?]", "recipient[gender]"
assert_select "input#recipient_race[name=?]", "recipient[race]"
assert_select "input#recipient_ethnicity[name=?]", "recipient[ethnicity]"
assert_select "input#recipient_home_language_id[name=?]", "recipient[home_language_id]"
assert_select "input#recipient_income[name=?]", "recipient[income]"
assert_select "input#recipient_opted_out[name=?]", "recipient[opted_out]"
assert_select "input#recipient_school_id[name=?]", "recipient[school_id]"
end
end
end

View file

@ -0,0 +1,30 @@
require 'rails_helper'
RSpec.describe "recipients/show", type: :view do
before(:each) do
@recipient = assign(:recipient, Recipient.create!(
:name => "Name",
:phone => "Phone",
:gender => "Gender",
:race => "Race",
:ethnicity => "Ethnicity",
:home_language_id => 2,
:income => "Income",
:opted_out => false,
:school_id => 3
))
end
it "renders attributes in <p>" do
render
expect(rendered).to match(/Name/)
expect(rendered).to match(/Phone/)
expect(rendered).to match(/Gender/)
expect(rendered).to match(/Race/)
expect(rendered).to match(/Ethnicity/)
expect(rendered).to match(/2/)
expect(rendered).to match(/Income/)
expect(rendered).to match(/false/)
expect(rendered).to match(/3/)
end
end