lundi 4 avril 2016

Refactor code using mock and stubb

I am newbie in RoR and i'm now trying to learn stubbs and mocks for testing. I have already done testing using rspec.Below is my controller code. Can anyone please explain me how can i convert this code using stubbs and mocks? RSpec.describe ArticlesController, type: :controller do
let(:article) { create :article} let(:art_params) { attributes_for(:article) }
let(:dbl) {double(:articles)} describe 'GET :new' do it 'render new template' do get :new expect(response).to render_template(:new) end end

describe 'POST/create' do
it 'created a new article ' do
expect { post :create, article: art_params }.to change(Article,      :count).by(1)
end
end

describe 'POST/create not' do
it 'did not create a new user' do
# expect(art_params).to receive(attributes_for :article).with(content:)
  art_params = { article: attributes_for(:article, content: nil) }
post :create, art_params
expect(response).to render_template(:new)
end

end

describe 'GET/edit' do it 'displays the edit template' do get :edit, id: article.id expect(response).to render_template(:edit) end end

describe 'POST/update' do it 'displays the update template' do post :update, id: article.id, article: attributes_for(:article) expect(response).to redirect_to(article_path(article.id)) end end

describe 'POST/DELETE' do it 'destroys the article template' do dbl = double() article = create :article expect { delete :destroy, id: article.id }.to change(Article, :count).by(-1)

end

end

Aucun commentaire:

Enregistrer un commentaire