簡體   English   中英

Rspec紅寶石滑軌

[英]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

我試圖找出導致此錯誤的原因,但我一直盯着它看了幾個小時,並試圖對其進行多次編輯以至無濟於事。 我不知道。 我一直在從事的項目的其余部分都還可以,但是我無法弄清楚為什么我無法獲得主題詞來成功轉換的原因。 謝謝參觀。

:topic替換為:params 這是您測試的預期關鍵字。 對於RSpec ,已經很清楚您正在測試Topic因為您的spec文件是TopicsController

問題在於post方法將關鍵字參數作為第二個參數。

如果需要指定params ,則應使用params關鍵字:

post :create, params: { topic: { name: ..., description: ... } }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM