Skip to content

Commit

Permalink
Adding specs
Browse files Browse the repository at this point in the history
  • Loading branch information
williamn committed Apr 24, 2012
1 parent 4b47230 commit 6cd9270
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 0 deletions.
171 changes: 171 additions & 0 deletions nifty-blog/spec/controllers/posts_controller_spec.rb
@@ -0,0 +1,171 @@
require 'spec_helper'

describe PostsController do

# This should return the minimal set of attributes required to create a valid
# Post. As you add validations to Post, be sure to
# update the return value of this method accordingly.
def valid_attributes
FactoryGirl.attributes_for(:post)
end

# 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
# PostsController. Be sure to keep this updated too.
def valid_session
{}
end

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
describe "with valid params" 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 "with invalid params" do
it "redirects to the posts list" do
get :show, {:id => 0}, valid_session
response.should redirect_to(posts_url)
end
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
describe "with valid params" 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 "with invalid params" do
it "redirects to the posts list" do
get :edit, {:id => 0}, valid_session
response.should redirect_to(posts_url)
end
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_attributes).with({'these' => 'params'})
put :update, {:id => post.to_param, :post => {'these' => 'params'}}, 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
describe "with valid params" 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

describe "with invalid params" do
it "redirects to the posts list" do
delete :destroy, {:id => 0}, valid_session
response.should redirect_to(posts_url)
end
end
end

end
7 changes: 7 additions & 0 deletions nifty-blog/spec/factories.rb
@@ -0,0 +1,7 @@
FactoryGirl.define do
factory :post do
title "MyTitle"
body "MyBody"
end
end

6 changes: 6 additions & 0 deletions nifty-blog/spec/models/post_spec.rb
@@ -0,0 +1,6 @@
require 'spec_helper'

describe Post do
it { should validate_presence_of(:title) }
it { should validate_presence_of(:body) }
end

0 comments on commit 6cd9270

Please sign in to comment.