2017-09-03 75 views
0

我有以下問題Ruby on Rails的v5.1.3沒有通過創建測試

失敗:

1)QuestionsController POST#創建與有效的屬性在數據庫中保存

問題
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#使用有效屬性重定向以顯示視圖

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

在0.46803秒成品(文件採取7.17秒加載) 10實施例中,2個故障

失敗的例子:

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 

這是我的控制器的代碼

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