Files
growstuff/spec/controllers/gardens_controller_spec.rb
Miles Gould bf58ff22a6 Give each user a default garden, called "Garden"
- created by an after_create callback on the user object
 - added test on model object
 - changed test of GET /gardens to expect Garden.all
2012-12-11 15:50:28 +00:00

167 lines
5.7 KiB
Ruby

require 'spec_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.
describe GardensController do
login_user
# This should return the minimal set of attributes required to create a valid
# Garden. As you add validations to Garden, be sure to
# update the return value of this method accordingly.
def valid_attributes
{:name => 'My Garden', :user_id => 1 }
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
# GardensController. Be sure to keep this updated too.
def valid_session
{}
end
describe "GET index" do
it "assigns all gardens as @gardens" do
gardens = Garden.all
get :index, {}
assigns(:gardens).should eq(gardens)
end
end
describe "GET show" do
it "assigns the requested garden as @garden" do
garden = Garden.create! valid_attributes
get :show, {:id => garden.to_param}
assigns(:garden).should eq(garden)
end
end
describe "GET new" do
it "assigns a new garden as @garden" do
get :new, {}
assigns(:garden).should be_a_new(Garden)
end
end
describe "GET edit" do
it "assigns the requested garden as @garden" do
garden = Garden.create! valid_attributes
get :edit, {:id => garden.to_param}
assigns(:garden).should eq(garden)
end
end
describe "POST create" do
describe "with valid params" do
it "creates a new Garden" do
expect {
post :create, {:garden => valid_attributes}
}.to change(Garden, :count).by(1)
end
it "assigns a newly created garden as @garden" do
post :create, {:garden => valid_attributes}
assigns(:garden).should be_a(Garden)
assigns(:garden).should be_persisted
end
it "redirects to the created garden" do
post :create, {:garden => valid_attributes}
response.should redirect_to(Garden.last)
end
end
describe "with invalid params" do
it "assigns a newly created but unsaved garden as @garden" do
# Trigger the behavior that occurs when invalid params are submitted
Garden.any_instance.stub(:save).and_return(false)
post :create, {:garden => {}}
assigns(:garden).should be_a_new(Garden)
end
it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Garden.any_instance.stub(:save).and_return(false)
post :create, {:garden => {}}
response.should render_template("new")
end
end
end
describe "PUT update" do
describe "with valid params" do
it "updates the requested garden" do
garden = Garden.create! valid_attributes
# Assuming there are no other gardens in the database, this
# specifies that the Garden created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Garden.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
put :update, {:id => garden.to_param, :garden => {'these' => 'params'}}
end
it "assigns the requested garden as @garden" do
garden = Garden.create! valid_attributes
put :update, {:id => garden.to_param, :garden => valid_attributes}
assigns(:garden).should eq(garden)
end
it "redirects to the garden" do
garden = Garden.create! valid_attributes
put :update, {:id => garden.to_param, :garden => valid_attributes}
response.should redirect_to(garden)
end
end
describe "with invalid params" do
it "assigns the garden as @garden" do
garden = Garden.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Garden.any_instance.stub(:save).and_return(false)
put :update, {:id => garden.to_param, :garden => {}}
assigns(:garden).should eq(garden)
end
it "re-renders the 'edit' template" do
garden = Garden.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Garden.any_instance.stub(:save).and_return(false)
put :update, {:id => garden.to_param, :garden => {}}
response.should render_template("edit")
end
end
end
describe "DELETE destroy" do
it "destroys the requested garden" do
garden = Garden.create! valid_attributes
expect {
delete :destroy, {:id => garden.to_param}
}.to change(Garden, :count).by(-1)
end
it "redirects to the gardens list" do
garden = Garden.create! valid_attributes
delete :destroy, {:id => garden.to_param}
response.should redirect_to(gardens_url)
end
end
end