Rspec ruby rails
Rspec ruby rails
我正在尝试创建一个正确设置的操作。
我一直收到错误消息: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
我试图找出导致此错误的原因,我已经盯着它看了好几个小时,并尝试多次编辑它,但都无济于事。我想不通。我一直在从事的项目的其余部分都还可以,但是我不明白为什么我无法成功转换主题一词。感谢您的观看。
问题是 post
方法将关键字参数作为第二个参数。
如果需要指定params
,则应使用params
关键字:
post :create, params: { topic: { name: ..., description: ... } }
将 :topic
替换为 :params
。这是您的测试的预期关键字。 RSpec
已经很清楚你正在测试 Topic
因为你的规范文件是 TopicsController
.
我正在尝试创建一个正确设置的操作。
我一直收到错误消息: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
我试图找出导致此错误的原因,我已经盯着它看了好几个小时,并尝试多次编辑它,但都无济于事。我想不通。我一直在从事的项目的其余部分都还可以,但是我不明白为什么我无法成功转换主题一词。感谢您的观看。
问题是 post
方法将关键字参数作为第二个参数。
如果需要指定params
,则应使用params
关键字:
post :create, params: { topic: { name: ..., description: ... } }
将 :topic
替换为 :params
。这是您的测试的预期关键字。 RSpec
已经很清楚你正在测试 Topic
因为你的规范文件是 TopicsController
.