contact: rewrite specs to use finer-grained mocks
Before the specs mocked entire controller methods–which caused them not to be tested. Now we use finer mocks (or no mocks at all), to actually run more code.
This commit is contained in:
parent
1d84ec7496
commit
151e5744e6
1 changed files with 40 additions and 27 deletions
|
@ -7,6 +7,7 @@ describe SupportController, type: :controller do
|
||||||
before do
|
before do
|
||||||
sign_in user
|
sign_in user
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:user) { create(:user) }
|
let(:user) { create(:user) }
|
||||||
|
|
||||||
it 'should not have email field' do
|
it 'should not have email field' do
|
||||||
|
@ -51,51 +52,64 @@ describe SupportController, type: :controller do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "send form" do
|
describe "send form" do
|
||||||
it 'should create conversation' do
|
subject do
|
||||||
expect(subject).not_to receive(:create_commentaire)
|
post :create, params: { subject: 'bonjour', text: 'un message' }
|
||||||
allow(subject).to receive(:create_conversation).and_return(true)
|
end
|
||||||
|
|
||||||
post :create, params: {
|
it 'creates a conversation on HelpScout' do
|
||||||
subject: 'bonjour',
|
expect_any_instance_of(Helpscout::FormAdapter).to receive(:send_form).and_return(true)
|
||||||
text: 'un message'
|
|
||||||
}
|
expect { subject }.to change(Commentaire, :count).by(0)
|
||||||
|
|
||||||
expect(flash[:notice]).to match('Votre message a été envoyé.')
|
expect(flash[:notice]).to match('Votre message a été envoyé.')
|
||||||
expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true)
|
expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true)
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with dossier" do
|
context 'when a drafted dossier is mentionned' do
|
||||||
let(:user) { dossier.user }
|
|
||||||
let(:dossier) { create(:dossier) }
|
let(:dossier) { create(:dossier) }
|
||||||
|
let(:user) { dossier.user }
|
||||||
|
|
||||||
it 'should create conversation' do
|
subject do
|
||||||
expect(subject).not_to receive(:create_commentaire)
|
|
||||||
allow(subject).to receive(:create_conversation).and_return(true)
|
|
||||||
|
|
||||||
post :create, params: {
|
post :create, params: {
|
||||||
dossier_id: dossier.id,
|
dossier_id: dossier.id,
|
||||||
type: Helpscout::FormAdapter::TYPE_INSTRUCTION,
|
type: Helpscout::FormAdapter::TYPE_INSTRUCTION,
|
||||||
subject: 'bonjour',
|
subject: 'bonjour',
|
||||||
text: 'un message'
|
text: 'un message'
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a conversation on HelpScout' do
|
||||||
|
expect_any_instance_of(Helpscout::FormAdapter).to receive(:send_form).and_return(true)
|
||||||
|
|
||||||
|
expect { subject }.to change(Commentaire, :count).by(0)
|
||||||
|
|
||||||
expect(flash[:notice]).to match('Votre message a été envoyé.')
|
expect(flash[:notice]).to match('Votre message a été envoyé.')
|
||||||
expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true)
|
expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context "en_construction" do
|
context 'when a submitted dossier is mentionned' do
|
||||||
let(:dossier) { create(:dossier, :en_construction) }
|
let(:dossier) { create(:dossier, :en_construction) }
|
||||||
|
let(:user) { dossier.user }
|
||||||
|
|
||||||
it 'should create commentaire' do
|
subject do
|
||||||
allow(subject).to receive(:create_commentaire).and_return(true)
|
|
||||||
expect(subject).not_to receive(:create_conversation)
|
|
||||||
|
|
||||||
post :create, params: {
|
post :create, params: {
|
||||||
dossier_id: dossier.id,
|
dossier_id: dossier.id,
|
||||||
type: Helpscout::FormAdapter::TYPE_INSTRUCTION,
|
type: Helpscout::FormAdapter::TYPE_INSTRUCTION,
|
||||||
subject: 'bonjour',
|
subject: 'bonjour',
|
||||||
text: 'un message'
|
text: 'un message'
|
||||||
}
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'posts the message to the dossier messagerie' do
|
||||||
|
expect_any_instance_of(Helpscout::FormAdapter).not_to receive(:send_form)
|
||||||
|
|
||||||
|
expect { subject }.to change(Commentaire, :count).by(1)
|
||||||
|
|
||||||
|
expect(Commentaire.last.email).to eq(user.email)
|
||||||
|
expect(Commentaire.last.dossier).to eq(dossier)
|
||||||
|
expect(Commentaire.last.body).to include('[bonjour]')
|
||||||
|
expect(Commentaire.last.body).to include('un message')
|
||||||
|
|
||||||
expect(flash[:notice]).to match('Votre message a été envoyé sur la messagerie de votre dossier.')
|
expect(flash[:notice]).to match('Votre message a été envoyé sur la messagerie de votre dossier.')
|
||||||
expect(response).to redirect_to messagerie_dossier_path(dossier)
|
expect(response).to redirect_to messagerie_dossier_path(dossier)
|
||||||
|
@ -103,7 +117,6 @@ describe SupportController, type: :controller do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
context 'signed out' do
|
context 'signed out' do
|
||||||
describe "with dossier" do
|
describe "with dossier" do
|
||||||
|
|
Loading…
Add table
Reference in a new issue