Skip to content

Commit

Permalink
boilerplate specs
Browse files Browse the repository at this point in the history
  • Loading branch information
matenia committed Jun 9, 2012
1 parent 429ee9b commit 7ffeed4
Show file tree
Hide file tree
Showing 9 changed files with 312 additions and 0 deletions.
164 changes: 164 additions & 0 deletions spec/controllers/notes_controller_spec.rb
@@ -0,0 +1,164 @@
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 NotesController do

# This should return the minimal set of attributes required to create a valid
# Note. As you add validations to Note, be sure to
# update the return value of this method accordingly.
def valid_attributes
{}
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
# NotesController. Be sure to keep this updated too.
def valid_session
{}
end

describe "GET index" do
it "assigns all notes as @notes" do
note = Note.create! valid_attributes
get :index, {}, valid_session
assigns(:notes).should eq([note])
end
end

describe "GET show" do
it "assigns the requested note as @note" do
note = Note.create! valid_attributes
get :show, {:id => note.to_param}, valid_session
assigns(:note).should eq(note)
end
end

describe "GET new" do
it "assigns a new note as @note" do
get :new, {}, valid_session
assigns(:note).should be_a_new(Note)
end
end

describe "GET edit" do
it "assigns the requested note as @note" do
note = Note.create! valid_attributes
get :edit, {:id => note.to_param}, valid_session
assigns(:note).should eq(note)
end
end

describe "POST create" do
describe "with valid params" do
it "creates a new Note" do
expect {
post :create, {:note => valid_attributes}, valid_session
}.to change(Note, :count).by(1)
end

it "assigns a newly created note as @note" do
post :create, {:note => valid_attributes}, valid_session
assigns(:note).should be_a(Note)
assigns(:note).should be_persisted
end

it "redirects to the created note" do
post :create, {:note => valid_attributes}, valid_session
response.should redirect_to(Note.last)
end
end

describe "with invalid params" do
it "assigns a newly created but unsaved note as @note" do
# Trigger the behavior that occurs when invalid params are submitted
Note.any_instance.stub(:save).and_return(false)
post :create, {:note => {}}, valid_session
assigns(:note).should be_a_new(Note)
end

it "re-renders the 'new' template" do
# Trigger the behavior that occurs when invalid params are submitted
Note.any_instance.stub(:save).and_return(false)
post :create, {:note => {}}, valid_session
response.should render_template("new")
end
end
end

describe "PUT update" do
describe "with valid params" do
it "updates the requested note" do
note = Note.create! valid_attributes
# Assuming there are no other notes in the database, this
# specifies that the Note created on the previous line
# receives the :update_attributes message with whatever params are
# submitted in the request.
Note.any_instance.should_receive(:update_attributes).with({'these' => 'params'})
put :update, {:id => note.to_param, :note => {'these' => 'params'}}, valid_session
end

it "assigns the requested note as @note" do
note = Note.create! valid_attributes
put :update, {:id => note.to_param, :note => valid_attributes}, valid_session
assigns(:note).should eq(note)
end

it "redirects to the note" do
note = Note.create! valid_attributes
put :update, {:id => note.to_param, :note => valid_attributes}, valid_session
response.should redirect_to(note)
end
end

describe "with invalid params" do
it "assigns the note as @note" do
note = Note.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Note.any_instance.stub(:save).and_return(false)
put :update, {:id => note.to_param, :note => {}}, valid_session
assigns(:note).should eq(note)
end

it "re-renders the 'edit' template" do
note = Note.create! valid_attributes
# Trigger the behavior that occurs when invalid params are submitted
Note.any_instance.stub(:save).and_return(false)
put :update, {:id => note.to_param, :note => {}}, valid_session
response.should render_template("edit")
end
end
end

describe "DELETE destroy" do
it "destroys the requested note" do
note = Note.create! valid_attributes
expect {
delete :destroy, {:id => note.to_param}, valid_session
}.to change(Note, :count).by(-1)
end

it "redirects to the notes list" do
note = Note.create! valid_attributes
delete :destroy, {:id => note.to_param}, valid_session
response.should redirect_to(notes_url)
end
end

end
15 changes: 15 additions & 0 deletions spec/helpers/notes_helper_spec.rb
@@ -0,0 +1,15 @@
require 'spec_helper'

# Specs in this file have access to a helper object that includes
# the NotesHelper. For example:
#
# describe NotesHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# helper.concat_strings("this","that").should == "this that"
# end
# end
# end
describe NotesHelper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/note_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Note do
pending "add some examples to (or delete) #{__FILE__}"
end
11 changes: 11 additions & 0 deletions spec/requests/notes_spec.rb
@@ -0,0 +1,11 @@
require 'spec_helper'

describe "Notes" do
describe "GET /notes" do
it "works! (now write some real specs)" do
# Run the generator again with the --webrat flag if you want to use webrat methods/matchers
get notes_path
response.status.should be(200)
end
end
end
35 changes: 35 additions & 0 deletions spec/routing/notes_routing_spec.rb
@@ -0,0 +1,35 @@
require "spec_helper"

describe NotesController do
describe "routing" do

it "routes to #index" do
get("/notes").should route_to("notes#index")
end

it "routes to #new" do
get("/notes/new").should route_to("notes#new")
end

it "routes to #show" do
get("/notes/1").should route_to("notes#show", :id => "1")
end

it "routes to #edit" do
get("/notes/1/edit").should route_to("notes#edit", :id => "1")
end

it "routes to #create" do
post("/notes").should route_to("notes#create")
end

it "routes to #update" do
put("/notes/1").should route_to("notes#update", :id => "1")
end

it "routes to #destroy" do
delete("/notes/1").should route_to("notes#destroy", :id => "1")
end

end
end
20 changes: 20 additions & 0 deletions spec/views/notes/edit.html.erb_spec.rb
@@ -0,0 +1,20 @@
require 'spec_helper'

describe "notes/edit" do
before(:each) do
@note = assign(:note, stub_model(Note,
:name => "MyString",
:tag => "MyString"
))
end

it "renders the edit note form" do
render

# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => notes_path(@note), :method => "post" do
assert_select "input#note_name", :name => "note[name]"
assert_select "input#note_tag", :name => "note[tag]"
end
end
end
24 changes: 24 additions & 0 deletions spec/views/notes/index.html.erb_spec.rb
@@ -0,0 +1,24 @@
require 'spec_helper'

describe "notes/index" do
before(:each) do
assign(:notes, [
stub_model(Note,
:name => "Name",
:tag => "Tag"
),
stub_model(Note,
:name => "Name",
:tag => "Tag"
)
])
end

it "renders a list of notes" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Name".to_s, :count => 2
# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "tr>td", :text => "Tag".to_s, :count => 2
end
end
20 changes: 20 additions & 0 deletions spec/views/notes/new.html.erb_spec.rb
@@ -0,0 +1,20 @@
require 'spec_helper'

describe "notes/new" do
before(:each) do
assign(:note, stub_model(Note,
:name => "MyString",
:tag => "MyString"
).as_new_record)
end

it "renders new note form" do
render

# Run the generator again with the --webrat flag if you want to use webrat matchers
assert_select "form", :action => notes_path, :method => "post" do
assert_select "input#note_name", :name => "note[name]"
assert_select "input#note_tag", :name => "note[tag]"
end
end
end
18 changes: 18 additions & 0 deletions spec/views/notes/show.html.erb_spec.rb
@@ -0,0 +1,18 @@
require 'spec_helper'

describe "notes/show" do
before(:each) do
@note = assign(:note, stub_model(Note,
:name => "Name",
:tag => "Tag"
))
end

it "renders attributes in <p>" do
render
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/Name/)
# Run the generator again with the --webrat flag if you want to use webrat matchers
rendered.should match(/Tag/)
end
end

0 comments on commit 7ffeed4

Please sign in to comment.