Skip to content

Instantly share code, notes, and snippets.

@tubbo
Forked from cored/posts_controller.rb
Last active December 28, 2015 21:59
Show Gist options
  • Select an option

  • Save tubbo/7568402 to your computer and use it in GitHub Desktop.

Select an option

Save tubbo/7568402 to your computer and use it in GitHub Desktop.
Failures:
1) PostsController POST create with valid params creates a new Post
Failure/Error: expect {
count should have been changed by 1, but was changed by 0
# ./spec/controllers/posts_controller_spec.rb:47:in `block (4 levels) in <top (required)>'
2) PostsController POST create with valid params assigns a newly created post as @post
Failure/Error: assigns(:post).should be_persisted
expected persisted? to return true, got false
# ./spec/controllers/posts_controller_spec.rb:55:in `block (4 levels) in <top (required)>'
3) PostsController POST create with valid params redirects to the created post
Failure/Error: response.should redirect_to(Post.last)
Expected response to be a <redirect>, but was <200>
# ./spec/controllers/posts_controller_spec.rb:60:in `block (4 levels) in <top (required)>'
Finished in 0.47945 seconds
16 examples, 3 failures
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
respond_with :html, :json
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
if @post.save
respond_with @post
else
respond_to do |format|
format.html { render 'new' }
format.json { render json: @post.errors, status: 422 }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params[:post].permit(:name, :description, :url, :bucket_id)
end
end
require 'spec_helper'
describe PostsController do
let(:bucket) do
FactoryGirl.create(:bucket, name: 'My Bucket')
end
let(:valid_attributes) do
FactoryGirl.attributes_for(:post, bucket: bucket)
end
let(:valid_session) { {} }
describe "GET index" do
it "assigns all posts as @posts" do
post = Post.create! valid_attributes
get :index, {}, valid_session
assigns(:posts).should eq([post])
end
end
describe "GET show" do
it "assigns the requested post as @post" do
post = Post.create! valid_attributes
get :show, {:id => post.to_param}, valid_session
assigns(:post).should eq(post)
end
end
describe "GET new" do
it "assigns a new post as @post" do
get :new, {}, valid_session
assigns(:post).should be_a_new(Post)
end
end
describe "GET edit" do
it "assigns the requested post as @post" do
post = Post.create! valid_attributes
get :edit, {:id => post.to_param}, valid_session
assigns(:post).should eq(post)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Post" do
expect {
post :create, {:post => valid_attributes}, valid_session
}.to change(Post, :count).by(1)
end
it "assigns a newly created post as @post" do
post :create, {:post => valid_attributes}, valid_session
assigns(:post).should be_a(Post)
assigns(:post).should be_persisted
end
it "redirects to the created post" do
post :create, {:post => valid_attributes}, valid_session
response.should redirect_to(Post.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved post as @post" do
# Trigger the behavior that occurs when invalid params are submitted
Post.any_instance.stub(:save).and_return(false)
post :create, {:post => { }}, valid_session
assigns(:post).should be_a_new(Post)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Post.any_instance.stub(:save).and_return(false)
post :create, {:post => { }}, valid_session
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested post" do
post = Post.create! valid_attributes
# Assuming there are no other posts in the database, this
# specifies that the Post created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Post.any_instance.should_receive(:update).with({ "name" => "updated" })
put :update, {:id => post.to_param, :post => { "name" => "updated" }}, valid_session
end
it "assigns the requested post as @post" do
post = Post.create! valid_attributes
put :update, {:id => post.to_param, :post => valid_attributes}, valid_session
assigns(:post).should eq(post)
end
it "redirects to the post" do
post = Post.create! valid_attributes
put :update, {:id => post.to_param, :post => valid_attributes}, valid_session
response.should redirect_to(post)
end
end
describe "with invalid params" do
it "assigns the post as @post" do
post = Post.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Post.any_instance.stub(:save).and_return(false)
put :update, {:id => post.to_param, :post => { }}, valid_session
assigns(:post).should eq(post)
end
it "re-renders the 'edit' template" do
post = Post.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Post.any_instance.stub(:save).and_return(false)
put :update, {:id => post.to_param, :post => { }}, valid_session
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested post" do
post = Post.create! valid_attributes
expect {
delete :destroy, {:id => post.to_param}, valid_session
}.to change(Post, :count).by(-1)
end
it "redirects to the posts list" do
post = Post.create! valid_attributes
delete :destroy, {:id => post.to_param}, valid_session
response.should redirect_to(posts_url)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment