-
Notifications
You must be signed in to change notification settings - Fork 0
4 controllers tests #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ksilex
wants to merge
4
commits into
3_tdd
Choose a base branch
from
4_controllers_tests
base: 3_tdd
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the answers controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: https://sass-lang.com/ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| // Place all the styles related to the questions controller here. | ||
| // They will automatically be included in application.css. | ||
| // You can use Sass (SCSS) here: https://sass-lang.com/ |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| class AnswersController < ApplicationController | ||
| def new | ||
| end | ||
|
|
||
| def create | ||
| @answer = question.answers.new(answer_params) | ||
| if @answer.save | ||
| redirect_to @answer.question | ||
| else | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def edit | ||
| end | ||
|
|
||
| def update | ||
| if answer.update(answer_params) | ||
| redirect_to answer.question | ||
| else | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| answer.destroy | ||
| redirect_to answer.question | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def answer | ||
| @answer ||= params[:id] ? Answer.find(params[:id]) : question.answers.new | ||
| end | ||
|
|
||
| helper_method :answer | ||
|
|
||
| def question | ||
| @question ||= Question.find(params[:question_id]) | ||
| end | ||
|
|
||
| def answer_params | ||
| params.require(:answer).permit(:body) | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| class QuestionsController < ApplicationController | ||
| def index | ||
| @questions = Question.all | ||
| end | ||
|
|
||
| def show | ||
| end | ||
|
|
||
| def new | ||
| end | ||
|
|
||
| def edit | ||
| end | ||
|
|
||
| def create | ||
| @question = Question.new(question_params) | ||
|
|
||
| if @question.save | ||
| redirect_to @question | ||
| else | ||
| render :new | ||
| end | ||
| end | ||
|
|
||
| def update | ||
| if question.update(question_params) | ||
| redirect_to @question | ||
| else | ||
| render :edit | ||
| end | ||
| end | ||
|
|
||
| def destroy | ||
| question.destroy | ||
| redirect_to questions_path | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def question | ||
| @question ||= params[:id] ? Question.find(params[:id]) : Question.new | ||
| end | ||
|
|
||
| helper_method :question | ||
|
|
||
| def question_params | ||
| params.require(:question).permit(:title, :body) | ||
| end | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module AnswersHelper | ||
| end |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| module QuestionsHelper | ||
| end |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,5 @@ | ||
| Rails.application.routes.draw do | ||
| # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html | ||
| resources :questions do | ||
| resources :answers, shallow: true, except: %i[index show] | ||
| end | ||
| end |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe AnswersController, type: :controller do | ||
| let!(:answer) { create(:answer) } | ||
|
|
||
| describe 'GET #new' do | ||
| before { get :new, params: { question_id: answer.question } } | ||
|
|
||
| it 'renders new view' do | ||
| expect(response).to render_template :new | ||
| end | ||
| end | ||
|
|
||
| describe 'POST #create' do | ||
| context 'with valid attributes' do | ||
| it 'saves a new answer in the database' do | ||
| expect { post :create, params: { answer: attributes_for(:answer), | ||
| question_id: answer.question } | ||
| }.to change(Answer, :count).by(1) | ||
| end | ||
|
|
||
| it 'redirects to associated question' do | ||
| post :create, params: { answer: attributes_for(:answer), question_id: answer.question } | ||
| expect(response).to redirect_to answer.question | ||
| end | ||
| end | ||
|
|
||
| context 'with invalid attributes' do | ||
| it 'does not save the answer' do | ||
| expect { post :create, params: { answer: attributes_for(:answer, :invalid_answer), | ||
| question_id: answer.question } | ||
| }.to_not change(Answer, :count) | ||
| end | ||
|
|
||
| it 're-renders new view' do | ||
| post :create, params: { answer: attributes_for(:answer, :invalid_answer), question_id: answer.question } | ||
| expect(response).to render_template :new | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'GET #edit' do | ||
| before { get :edit, params: { id: answer } } | ||
|
|
||
| it 'renders edit view' do | ||
| expect(response).to render_template :edit | ||
| end | ||
| end | ||
|
|
||
| describe 'PATCH #update' do | ||
| context 'with valid attributes' do | ||
| it 'changes answer attributes' do | ||
| patch :update, params: { id: answer, answer: { body: 'new body' } } | ||
| answer.reload | ||
| expect(answer.body).to eq 'new body' | ||
| end | ||
|
|
||
| it 'redirects to associated question' do | ||
| patch :update, params: { id: answer, answer: attributes_for(:answer) } | ||
| expect(response).to redirect_to answer.question | ||
| end | ||
| end | ||
|
|
||
| context 'with invalid attributes' do | ||
| before { patch :update, params: { id: answer, answer: attributes_for(:answer, :invalid_answer) } } | ||
|
|
||
| it 'does not change answer' do | ||
| answer.reload | ||
| expect(answer.body).to eq "MyText" | ||
| end | ||
|
|
||
| it 're-renders edit view' do | ||
| expect(response).to render_template :edit | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'DELETE #destroy' do | ||
| it 'deletes the answer' do | ||
| expect { delete :destroy, params: { id: answer } }.to change(Answer, :count).by(-1) | ||
| end | ||
|
|
||
| it 'redirects to associated question' do | ||
| delete :destroy, params: { id: answer } | ||
| expect(response).to redirect_to answer.question | ||
| end | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
тут стоит в change указать question.answers, чтобы сразу проверить, что ответ связывается с вопросом