Created
September 16, 2013 23:13
-
-
Save fairweatherfan/6587927 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe ArticlesController do | |
let(:article) { build_stubbed(:article) } | |
describe 'GET #index' do | |
before do | |
Article.stub all: [article] | |
get :index | |
end | |
it 'gets all articles' do | |
expect(Article).to have_received(:all) | |
end | |
it 'assigns @articles' do | |
expect(assigns(:articles)).to eq [article] | |
end | |
it 'responds successfully' do | |
expect(response).to be_success | |
end | |
it 'renders :index template' do | |
expect(response).to render_template(:index) | |
end | |
end | |
describe 'GET #show' do | |
before do | |
Article.stub find: article | |
get :show, id: article | |
end | |
it 'finds article by id' do | |
expect(Article).to have_received(:find).with(article.id.to_s) | |
end | |
it 'assigns @article' do | |
expect(assigns(:article)).to eq(article) | |
end | |
it 'responds successfully' do | |
expect(response).to be_success | |
end | |
it 'renders :show template' do | |
expect(response).to render_template(:show) | |
end | |
end | |
describe 'GET #new' do | |
let(:new_article) { mock_model(Article) } | |
before do | |
Article.stub new: new_article | |
get :new | |
end | |
it 'builds new article' do | |
expect(Article).to have_received(:new) | |
end | |
it 'assigns new article to @article' do | |
expect(assigns(:article)).to eq(new_article) | |
end | |
it 'responds successfully' do | |
expect(response).to be_success | |
end | |
it 'renders :new template' do | |
expect(response).to render_template(:new) | |
end | |
end | |
describe 'GET #edit' do | |
before do | |
Article.stub find: article | |
get :edit, id: article | |
end | |
it 'finds article by id' do | |
expect(Article).to have_received(:find).with(article.id.to_s) | |
end | |
it 'assigns @article' do | |
expect(assigns(:article)).to eq(article) | |
end | |
it 'responds successfully' do | |
expect(response).to be_success | |
end | |
it 'renders :edit template' do | |
expect(response).to render_template(:edit) | |
end | |
end | |
describe 'POST #create' do | |
let(:new_article) { new_article = mock_model(Article) } | |
context 'with valid attributes' do | |
before do | |
Article.stub new: new_article | |
new_article.stub save: true | |
post :create, article: attributes_for(:article) | |
end | |
it 'calls #save on the article' do | |
expect(new_article).to have_received(:save) | |
end | |
it 'redirects to created article' do | |
expect(response).to redirect_to(new_article) | |
end | |
end | |
context 'with invalid attributes' do | |
before do | |
Article.stub new: new_article | |
new_article.stub save: false | |
post :create, article: attributes_for(:article) | |
end | |
it 'calls #save on the article' do | |
expect(new_article).to have_received(:save) | |
end | |
it 'renders :new template' do | |
expect(response).to render_template(:new) | |
end | |
it 'assigns new article to @article' do | |
expect(assigns(:article)).to eq(new_article) | |
end | |
end | |
end | |
describe 'PATCH #update' do | |
context 'with valid attributes' do | |
let(:article_params) do | |
{ 'title' => 'Test title', 'content' => 'Lorem Ipsum' } | |
end | |
before do | |
Article.stub find: article | |
article.stub update: true | |
patch :update, id: article, article: article_params | |
end | |
it 'finds the article by id' do | |
expect(Article).to have_received(:find).with(article.id.to_s) | |
end | |
it 'calls #update on the article' do | |
expect(article).to have_received(:update).with(article_params) | |
end | |
it 'redirects to the updated article' do | |
expect(response).to redirect_to(article) | |
end | |
end | |
context 'with invalid attributes' do | |
let(:article_params) do | |
{ 'title' => 'Test title', 'content' => '' } | |
end | |
before do | |
Article.stub find: article | |
article.stub update: false | |
patch :update, id: article, article: article_params | |
end | |
it 'finds the article by id' do | |
expect(Article).to have_received(:find).with(article.id.to_s) | |
end | |
it 'calls #update on the article' do | |
expect(article).to have_received(:update).with(article_params) | |
end | |
it 'renders :edit template' do | |
expect(response).to render_template(:edit) | |
end | |
end | |
end | |
describe 'DELETE #destroy' do | |
before do | |
Article.stub find: article | |
article.stub destroy: true | |
delete :destroy, id: article | |
end | |
it 'finds the article by id' do | |
expect(Article).to have_received(:find).with(article.id.to_s) | |
end | |
it 'calls #destroy on the article' do | |
expect(article).to have_received(:destroy) | |
end | |
it 'redirects to articles' do | |
expect(response).to redirect_to(articles_path) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wanted a reference to a good succinct controller spec that mocks or stubs most actions to keep unit tests independent and upon which to build more complex controller specs as required.