Skip to content

Instantly share code, notes, and snippets.

@s2t2
Last active September 23, 2021 13:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2t2/6498719 to your computer and use it in GitHub Desktop.
Save s2t2/6498719 to your computer and use it in GitHub Desktop.
files generated by rails scaffold controller (for reference)
<%= form_for(@thing) do |f| %>
<% if @thing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@thing.errors.count, "error") %> prohibited this thing from being saved:</h2>
<ul>
<% @thing.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :source_url %><br>
<%= f.text_area :source_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
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 AgenciesController, :type => :controller do
# This should return the minimal set of attributes required to create a valid
# Agency. As you add validations to Agency, 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
# AgenciesController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe "GET index" do
it "assigns all agencies as @agencies" do
agency = Agency.create! valid_attributes
get :index, {}, valid_session
expect(assigns(:agencies)).to eq([agency])
end
end
describe "GET show" do
it "assigns the requested agency as @agency" do
agency = Agency.create! valid_attributes
get :show, {:id => agency.to_param}, valid_session
expect(assigns(:agency)).to eq(agency)
end
end
describe "GET new" do
it "assigns a new agency as @agency" do
get :new, {}, valid_session
expect(assigns(:agency)).to be_a_new(Agency)
end
end
describe "GET edit" do
it "assigns the requested agency as @agency" do
agency = Agency.create! valid_attributes
get :edit, {:id => agency.to_param}, valid_session
expect(assigns(:agency)).to eq(agency)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Agency" do
expect {
post :create, {:agency => valid_attributes}, valid_session
}.to change(Agency, :count).by(1)
end
it "assigns a newly created agency as @agency" do
post :create, {:agency => valid_attributes}, valid_session
expect(assigns(:agency)).to be_a(Agency)
expect(assigns(:agency)).to be_persisted
end
it "redirects to the created agency" do
post :create, {:agency => valid_attributes}, valid_session
expect(response).to redirect_to(Agency.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved agency as @agency" do
post :create, {:agency => invalid_attributes}, valid_session
expect(assigns(:agency)).to be_a_new(Agency)
end
it "re-renders the 'new' template" do
post :create, {:agency => invalid_attributes}, valid_session
expect(response).to render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
let(:new_attributes) {
skip("Add a hash of attributes valid for your model")
}
it "updates the requested agency" do
agency = Agency.create! valid_attributes
put :update, {:id => agency.to_param, :agency => new_attributes}, valid_session
agency.reload
skip("Add assertions for updated state")
end
it "assigns the requested agency as @agency" do
agency = Agency.create! valid_attributes
put :update, {:id => agency.to_param, :agency => valid_attributes}, valid_session
expect(assigns(:agency)).to eq(agency)
end
it "redirects to the agency" do
agency = Agency.create! valid_attributes
put :update, {:id => agency.to_param, :agency => valid_attributes}, valid_session
expect(response).to redirect_to(agency)
end
end
describe "with invalid params" do
it "assigns the agency as @agency" do
agency = Agency.create! valid_attributes
put :update, {:id => agency.to_param, :agency => invalid_attributes}, valid_session
expect(assigns(:agency)).to eq(agency)
end
it "re-renders the 'edit' template" do
agency = Agency.create! valid_attributes
put :update, {:id => agency.to_param, :agency => invalid_attributes}, valid_session
expect(response).to render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested agency" do
agency = Agency.create! valid_attributes
expect {
delete :destroy, {:id => agency.to_param}, valid_session
}.to change(Agency, :count).by(-1)
end
it "redirects to the agencies list" do
agency = Agency.create! valid_attributes
delete :destroy, {:id => agency.to_param}, valid_session
expect(response).to redirect_to(agencies_url)
end
end
end
require "rails_helper"
RSpec.describe AgenciesController, :type => :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/agencies").to route_to("agencies#index")
end
it "routes to #new" do
expect(:get => "/agencies/new").to route_to("agencies#new")
end
it "routes to #show" do
expect(:get => "/agencies/1").to route_to("agencies#show", :id => "1")
end
it "routes to #edit" do
expect(:get => "/agencies/1/edit").to route_to("agencies#edit", :id => "1")
end
it "routes to #create" do
expect(:post => "/agencies").to route_to("agencies#create")
end
it "routes to #update" do
expect(:put => "/agencies/1").to route_to("agencies#update", :id => "1")
end
it "routes to #destroy" do
expect(:delete => "/agencies/1").to route_to("agencies#destroy", :id => "1")
end
end
end
require 'rails_helper'
RSpec.describe "Agencies", :type => :request do
describe "GET /agencies" do
it "works! (now write some real specs)" do
get agencies_path
expect(response).to have_http_status(200)
end
end
end
<h1>Editing thing</h1>
<%= render 'form' %>
<%= link_to 'Show', @thing %> |
<%= link_to 'Back', things_path %>
require 'rails_helper'
RSpec.describe "agencies/index", :type => :view do
before(:each) do
assign(:agencies, [
Agency.create!(),
Agency.create!()
])
end
it "renders a list of agencies" do
render
end
end
json.array!(@agencies) do |agency|
json.extract! agency, :id
json.url agency_url(agency, format: :json)
end
<p id="notice"><%= notice %></p>
<%= link_to 'Edit', edit_thing_path(@thing) %> |
<%= link_to 'Back', things_path %>
require 'rails_helper'
RSpec.describe "agencies/show", :type => :view do
before(:each) do
@agency = assign(:agency, Agency.create!())
end
it "renders attributes in <p>" do
render
end
end
json.extract! @agency, :id, :created_at, :updated_at
class ThingsController < ApplicationController
before_action :set_thing, only: [:show, :edit, :update, :destroy]
# GET /things
def index
@things = Thing.all
end
# GET /things/1
def show
end
# GET /things/new
def new
@thing = Thing.new
end
# GET /things/1/edit
def edit
end
# POST /things
def create
@thing = Thing.new(thing_params)
if @thing.save
redirect_to @thing, notice: 'Thing was successfully created.'
else
render action: 'new'
end
end
# PATCH/PUT /things/1
def update
if @thing.update(thing_params)
redirect_to @thing, notice: 'Thing was successfully updated.'
else
render action: 'edit'
end
end
# DELETE /things/1
def destroy
@thing.destroy
redirect_to things_url, notice: 'Thing was successfully destroyed.'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_thing
@thing = Thing.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def thing_params
params[:thing]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment