I have the following problem
Failures:
1) QuestionsController
POST #create with valid attributes saves the question in the database
Failure/Error: post :create, question: attributes_for(:question) ArgumentError: unknown keyword: question # ./spec/controllers/questions_controller_spec.rb:68:in `block (4 levels) in <top (required)>'
2) QuestionsController POST #create with valid attributes redirect to show view
Failure/Error: post :create, question: attributes_for(:question) ArgumentError: unknown keyword: question # ./spec/controllers/questions_controller_spec.rb:73:in `block (4 levels) in <top (required)>'
Finished in 0.46803 seconds (files took 7.17 seconds to load) 10 examples, 2 failures
Failed examples:
rspec ./spec/controllers/questions_controller_spec.rb:66 # QuestionsController POST #create with valid attributes saves the question in the database
rspec ./spec/controllers/questions_controller_spec.rb:72 # QuestionsController POST #create with valid attributes redirect to show view
here is the code of my controller
class QuestionsController < ApplicationController
before_action :load_question, only: [:show, :edit]
def index
@questions = Question.all
end
def show
end
def new
@question = Question.new
end
def edit
end
private
def load_question
@question = Question.find(params[:id])
end
def create
@question = Question.create(question_params)
redirect_to @question
end
def question_params
params.require(:question).permit(:title, :body)
end
end
And here's the spec question_controller_spec.rb
require 'rails_helper'
RSpec.describe QuestionsController, type: :controller do
let(:question) {FactoryGirl.create(:question)}
describe "GET #index" do
let(:questions) {FactoryGirl.create_list(:question, 2)}
before do
#@questions = FactoryGirl.create_list(:question, 2) #в фабрике создаем вопросы
get :index #вызываем экшн index
end
it 'populates an array ot all questions' do #должен заполнить в массив все вопросы которые вводятся
expect(assigns(:questions)).to match_array(questions) #проверяем в переменой questions присутствует массив из question1 и 2
end
it 'renders index view' do #должен отрендерит экшн view
expect(response).to render_template :index #ожидает ,что ответ от контроллера совпадает с нашим экшном index
end
end
describe "GET #show" do
#let(:question) {FactoryGirl.create(:question)}let(:question) {FactoryGirl.create(:question)}#создаем вопрос
before do
get :show, params: {id: question.id}
end
it 'assings the requested question to question' do #должен установливать рапрошенный вопрос
#вызиваем экшн show с параметром id ,то есть соответствующий вопрос
expect(assigns(:question)).to eq question
end
it 'renders show view' do
expect(response).to render_template :show
end
end
describe "GET #new" do
before do
get :new
end
it 'assingns a New Question to @question' do #создает новый вопрос
expect(assigns(:question)).to be_a_new(Question)
end
it 'renders new views' do
expect(response).to render_template :new
end
end
describe "GET #edit" do
#let(:question) {FactoryGirl.create(:question)}
before do
get :edit, params: {id: question.id}
end
it 'assings the requested question to question' do
expect(assigns(:question)).to eq question
end
it 'render new view' do
expect(response).to render_template :edit
end
end
describe "POST #create" do
context "with valid attributes" do
let(:question) {create{:question}}
it 'saves the question in the database' do #должен сохранить вопрос в БД, если оно валидний
old_count = Question.count
post :create, question: attributes_for(:question)
#expect { post :create, question: FactoryGirl.attributes_for(:question) } .to change(Question, :count).by(1)
expect(Question.count).to eq (old_count + 1) # таким способом проверяется ,что добавился вопрос в БД,то есть количество возростло на 1
end
it 'redirect to show view' do
post :create, question: attributes_for(:question)
expect(response).to redirect_to question_path(assigns(:question))
end
end
end
end
I can not find where is my error
There are also routes
C:\Prom\qna>rake routes
Prefix Verb URI Pattern Controller#Action
questions GET /questions(.:format) questions#index
POST /questions(.:format) questions#create
new_question GET /questions/new(.:format) questions#new
edit_question GET /questions/:id/edit(.:format) questions#edit
question GET /questions/:id(.:format) questions#show
PATCH /questions/:id(.:format) questions#update
PUT /questions/:id(.:format) questions#update
DELETE /questions/:id(.:format) questions#destroy