Reputation: 57
I'm working on a project for my bootcamp and I cannot fathom why the tests are failing but the action correctly works. The tests:
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
The create action:
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 check my tests:
rspec spec/controllers/topics_controller_spec.rb -e 'POST create'
I get three failures.
Failures:
1) TopicsController POST create increases the number of topics by 1
Failure/Error: expect{ post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph }}}.to change(Topic, :count).by(1)
ArgumentError:
unknown keyword: topic
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/topics_controller_spec.rb:54:in `block (4 levels) in <top (required)>'
# ./spec/controllers/topics_controller_spec.rb:54:in `block (3 levels) in <top (required)>'
2) TopicsController POST create assigns Topic.last to @topic
Failure/Error: post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
ArgumentError:
unknown keyword: topic
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/topics_controller_spec.rb:58:in `block (3 levels) in <top (required)>'
3) TopicsController POST create redirects to new topic
Failure/Error: post :create, {topic: {name: RandomData.random_sentence, description: RandomData.random_paragraph}}
ArgumentError:
unknown keyword: topic
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/template_assertions.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.4.0/gems/rails-controller-testing-1.0.2/lib/rails/controller/testing/integration.rb:12:in `block (2 levels) in <module:Integration>'
# ./spec/controllers/topics_controller_spec.rb:63:in `block (3 levels) in <top (required)>'
Finished in 0.02654 seconds (files took 2.16 seconds to load)
3 examples, 3 failures
Failed examples:
rspec ./spec/controllers/topics_controller_spec.rb:53 # TopicsController POST create increases the number of topics by 1
rspec ./spec/controllers/topics_controller_spec.rb:57 # TopicsController POST create assigns Topic.last to @topic
rspec ./spec/controllers/topics_controller_spec.rb:62 # TopicsController POST create redirects to new topic
The error is telling me that there is an issue with the unknown keyword "topic" yet that keyword works fine in the other 7 tests in my topics_controller_spec.rb file. I have been staring at this for over an hour and cannot fathom for the life of me where I went wrong.
Upvotes: 0
Views: 53
Reputation: 3300
I think you picked up an old example.
You would need to add params
with the params key. Try this
describe "POST create" do
it "increases the number of topics by 1" do
expect{ post :create, params: {topic: ...} }.to change(Topic,:count).by(1)
end
end
Upvotes: 1