我有以下问题
故障:
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,有效属性重定向到显示视图
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 :(得分:0)
你应该说明问题是正确的。您的问题不是没有创建记录,而是测试代码本身失败。如果您使用Google,first answer会告诉您,根据标准,您需要将所有参数放在params
哈希中。所以你应该使用
post :create, :params => { :question => attributes_for(question) }