2017-09-03 5 views
0

I次のような問題にのRuby on Railsのv5.1.3作成テスト

障害があります。有効な属性を持つ

1)QuestionsController POSTの#createデータベース

に質問をセーブします
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が

0ビューを表示するためのリダイレクトここ

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 

は私のコントローラ

のコードです:0.46803秒(ファイルをロードするために7.17秒かかった) 10例、2つの障害

失敗例に仕上がり

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)>' 

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 

スペック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 

私は私の誤差がある

を見つけることができないルートは、あなたが右の問題を述べるべきで

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 

答えて

0

もあります。あなたの問題は、レコードは作成されていませんが、そのテストコード自体は失敗します。 Googleを使用している場合、first answerは、標準に従って、すべてのパラメータをparamsハッシュにする必要があることを伝えます。だからあなたが使用する必要があります

post :create, :params => { :question => attributes_for(question) }