Skip to content

Commit

Permalink
Refactored Tests to use let() method, and use right relations in orde…
Browse files Browse the repository at this point in the history
…r to simulate required app conditions to test the controllers and models.
  • Loading branch information
Gerson Scanapieco committed Oct 12, 2013
1 parent e122b03 commit 3287cd7
Show file tree
Hide file tree
Showing 11 changed files with 228 additions and 75 deletions.
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
root 'welcome#index'

namespace :admin do
resources :level
resources :level do
resources :comments
resources :questions, as: "bonus_questions"
resources :topics, shallow: true
Expand Down
85 changes: 55 additions & 30 deletions spec/controllers/comments_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
require 'spec_helper'

describe CommentsController do
describe Admin::CommentsController do
#login(:admin)

before(:each) do
let(:level) { FactoryGirl.create(:level)}
end

describe "GET #index" do
it "populates an array of comments" do
comments = 2.times.create(:comment)
get :index
level.comments << comments

get :index, level_id: level.id
page_comments = assigns(:comments)

expect(comments).to eq(page_comments)
end

it "renders the :index view" do
comment = create(:comment)
get :index
level.comments << comment

get :index, level_id: level.id

expect(response).to render_template :index
end
Expand All @@ -24,31 +33,39 @@
context "when comment is found" do
it "assigns the requested Comment to @comment" do
comment = create(:comment)
level.comments << comment

get :show, id: comment.id
get :show, id: comment.id, level_id: level.id
comment1 = assigns[:comment]

expect(comment).to eq comment1
end

it "renders the :show template" do
comment = create(:comment)

get :show, id: comment.id
level.comments << comment

get :show, id: comment.id, level_id: level.id

expect(response).to render_template :show
end
end

context "when comment is not found" do
it "shows an error message" do
get :show, id: "test"
comment = create(:comment)
level.comments << comment

get :show, id: "test_id", level_id: level.id

expect(flash[:error]).to eq "Comment could not be found"
end

it "redirects to index view" do
get :show, id: "test"
comment = create(:comment)
level.comments << comment

get :show, id: "test_id", level_id: level.id

expect(reponse).to redirect_to :index
end
Expand All @@ -59,14 +76,14 @@
it "assigns a new Comment to @comment" do
comment = build(:comment)

get :new
get :new, level_id: level.id

comment1 = assigns[:comment]
expect(comment1).to eq comment
end

it "renders the :new template" do
get :new
get :new, level_id: level.id

expect(reponse).to render_template :new
end
Expand All @@ -76,7 +93,7 @@
it "assigns the requested Comment to @comment" do
comment = create(:comment)

get :edit, id: comment.id
get :edit, id: comment.id, level_id: level.id
comment1 = assigns[:comment]

expect(comment).to eq comment1
Expand All @@ -85,7 +102,7 @@
it "renders the :edit template" do
comment = create(:comment)

get :edit, id: comment.id
get :edit, id: comment.id, level_id: level.id

expect(response).to render_template :edit
end
Expand All @@ -96,37 +113,36 @@
it "saves the new Comment in the database" do
attrs = attributes_for(:comment)

expect { post :create, id: comment.id, comment: attrs}.to change(Comment,:count).by(1)
expect { post :create, level_id: level.id, comment: attrs}.to change(Comment,:count).by(1)
end

it "redirects to the :index view" do
attrs = attributes_for(:comment)

post :create, comment: attrs
comment = assigns[:comment]
post :create, level_id: level.id, comment: attrs

expect(reponse).to redirect_to :index
expect(reponse).to redirect_to :show
end
end

context "with invalid attributes" do
it "doesn't save the new Comment in the database" do
invalid_comment = create(:comment, text: nil)
post :create, invalid_comment
invalid_attrs = attributes_for(:comment, text: nil)
post :create, comment: invalid_attrs, level_id: level.id

expect(Comment.count).to_not change
end

it "redirects to the :new view" do
invalid_comment = create(:comment, text: nil)
post :create, invalid_comment
invalid_attrs = attributes_for(:comment, text: nil)
post :create, comment: invalid_attrs, level_id: level.id

expect(response).to redirect_to :new
end

it "shows an error message" do
invalid_comment = create(:comment, text: nil)
post :create, invalid_comment
invalid_attrs = attributes_for(:comment, text: nil)
post :create, comment: invalid_attrs, level_id: level.id

expect(flash[:error]).to eq "Comment could not be created"
end
Expand All @@ -139,7 +155,7 @@
comment = create(:comment)
attrs = attributes_for(:comment, text: "updated")

put :update, comment.id, comment: attrs
put :update, id: comment.id, level_id: level.id, comment: attrs

updated_comment = assigns[:comment]

Expand All @@ -150,7 +166,7 @@
comment = create(:comment)
attrs = attributes_for(:comment, text: "updated")

put :update, comment.id, comment: attrs
put :update, id: comment.id, level_id: level.id, comment: attrs

updated_comment = assigns[:comment]

Expand All @@ -161,7 +177,7 @@
comment = create(:comment)
attrs = attributes_for(:comment, text: "updated")

put :update, comment.id, comment: attrs
put :update, id: comment.id, level_id: level.id, comment: attrs

expect(flash[:notice]). to eq "Comment successfully updated"
end
Expand All @@ -172,7 +188,7 @@
comment = create(:comment)
attrs = attributes_for(:comment, text: nil)

put :update, comment.id, comment: attrs
put :update, id: comment.id, level_id: level.id, comment: attrs

updated_comment = assigns[:comment]

Expand All @@ -183,7 +199,7 @@
comment = create(:comment)
attrs = attributes_for(:comment, text: nil)

put :update, comment.id, comment: attrs
put :update, id: comment.id, level_id: level.id, comment: attrs

expect(reponse).to redirect_to :index
end
Expand All @@ -192,7 +208,7 @@
comment = create(:comment)
attrs = attributes_for(:comment, text: nil)

put :update, comment.id, comment: attrs
put :update, id: comment.id, level_id: level.id, comment: attrs

expect(flash[:error]). to eq "Comment could not be updated"
end
Expand All @@ -203,28 +219,37 @@
context "when find the comment" do
it "deletes the comment" do
comment = create(:comment)
level.comments << comment

delete :destroy, id: comment.id
delete :destroy, id: comment.id, level_id: level.id

expect(Comment.count).to eq 0
end

it "redirects to the :index view" do
comment = create(:comment)

delete :destroy, id: comment.id
delete :destroy, id: comment.id, level_id: level.id

expect(response).to redirect_to :index
end
end

context "when can not find the comment" do
it "shows an error message" do
comment = create(:comment)

delete :destroy, level_id: level.id, id: "test"

expect(flash[:error]).to eql "Could not delete comment. Comment inexistent."
end

it "redirects to index view" do
comment = create(:comment)

delete :destroy, level_id: level.id, id: "test"

expect(response).to redirect_to :index
end
end
end
Expand Down
11 changes: 8 additions & 3 deletions spec/controllers/contents_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@

describe ContentsController do
let(:content) { FactoryGirl.create(:content) }
let(:topic) {FactoryGirl.create(:topic)}

describe "GET #show" do
it "assigns the requested Content to @content" do
get :show, id: content.id
topic.contents << content

get :show, id: content.id, topic_id: topic.id
page_content = assigns[:content]

expect(content).to eq page_content
end

it "renders the :show template" do
get :show, id: content.id
topic.contents << content

get :show, id: content.id, topic_id: topic.id

expect(response).to render_template :show
end
end

it 'can download a mp3 audio file'
it 'can download an ogg audio file'
end
end
41 changes: 41 additions & 0 deletions spec/controllers/feedbacks_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe "FeedbacksController" do

describe "GET #new" do
it "assigns a new feedback object" do
get :new
feedback = assigns[:feedback]

expect(feedback).not_to be_nil
end

it "renders the :new view" do
get :new

expect(response).to render_template :new
end
end

describe "POST #create" do
before do
let(:attrs) { FactoryGirl.attributes_for(:feedback) }
end

it "creates a new database record" do
expect{ put :create, feedback: attrs}.to change(Feedback,:count).by(1)
end

it "redirects to :show view" do
put :create, feedback: attrs

expect(response).to redirect_to :show
end

it "shows a success message" do
put :create, feedback: attrs

expect(flash[:notice]).to eql "Feedback successfully created"
end
end
end
41 changes: 41 additions & 0 deletions spec/controllers/invitations_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'spec_helper'

describe "FeedbacksController" do

describe "GET #new" do
it "assigns a new feedback object" do
get :new
invitation = assigns[:invitation]

expect(invitation).not_to be_nil
end

it "renders the :new view" do
get :new

expect(response).to render_template :new
end
end

describe "POST #create" do
before do
let(:attrs) {FactoryGirl.attributes_for(:invitation)}
end

it "creates a new database record" do
expect{ put :create, invitation: attrs}.to change(Invitation,:count).by(1)
end

it "redirects to :show view" do
put :create, invitation: attrs

expect(response).to redirect_to :show
end

it "shows a success message" do
put :create, invitation: attrs

expect(flash[:notice]).to eql "Invitation successfully created"
end
end
end
2 changes: 0 additions & 2 deletions spec/controllers/levels_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

describe LevelsController do
before(:each) do
let(:user) { FactoryGirl.create(:user) }
let(:level) { FactoryGirl.create(:level) }
let(:question) { FactoryGirl.create(:question) }
end

describe "GET #show" do
Expand Down
Loading

0 comments on commit 3287cd7

Please sign in to comment.