我正在尝试正确设置创建操作。
我一直收到错误:ArgumentError: Unknown keyword: topic
以下是测试:
require 'rails_helper'
RSpec.describe TopicsController, type: :controller do
let(:my_topic) { Topic.create!(name: RandomData.random_sentence, description: RandomData.random_paragraph)}
describe "POST create" do
it "increases the number of topics by 1" do
expect{ post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}}.to change(Topic,:count).by(1)
end
it "assigns Topic.last to @topic" do
post :create, { topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
expect(assigns(:topic)).to eq Topic.last
end
it "redirects to the new topic" do
post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
expect(response).to redirect_to Topic.last
end
end
这是控制器:
def create
@topic = Topic.new
@topic.name = params[:topic][:name]
@topic.description = params[:topic][:description]
@topic.public = params[:topic][:public]
if @topic.save
redirect_to @topic, notice: "Topic was saved successfully."
else
flash.now[:alert] = "Error creating topic. Please try again"
render :new
end
end
我正在试图弄清楚我错过了什么导致了这个错误我已经盯着它好几个小时并试图多次编辑它无济于事。我无法弄清楚。我一直在努力的项目的其余部分一直没问题,但我无法弄清楚为什么我不能成功转换单词主题。谢谢你看看。
答案 0 :(得分:1)
将:topic
替换为:params
。这是您测试的预期关键字。由于您的规范文件为RSpec
,因此Topic
已经清楚您正在测试TopicsController
。
答案 1 :(得分:0)
问题是post
方法将关键字参数作为第二个参数。
如果您需要指定params
,则应使用params
关键字:
post :create, params: { topic: { name: ..., description: ... } }