user6378390
user6378390

Reputation:

Rspec ruby rails

I'm trying to get a create action to set up properly.

I keep getting an error: ArgumentError: Unknown keyword: topic

Here is the testing:

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

Here is the controller:

  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

I'm trying to figure out what I'm missing that is causing this error I've been staring at it for hours and have tried to edit it multiple times to no avail. I can't figure it out. The rest of the project I've been working on has been okay however I cannot figure out why I can't get the word topic to convert successfully. Thanks for taking a look.

Upvotes: 0

Views: 59

Answers (2)

ydaniju
ydaniju

Reputation: 400

Replace :topic with :params. That's the expected keyword for your test. It is already clear to RSpec that you're testing for Topic since your spec file is TopicsController.

Upvotes: 1

Igor Drozdov
Igor Drozdov

Reputation: 15045

The problem is that the post method takes keyword arguments as a second argument.

If you need to specify params, the params keyword should be used:

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

Upvotes: 0

Related Questions