I am writing some tests for one of my controllers, and all of them are passing except the test for my #create method.
I cannot seem to get this test to pass. I understand that the UnknownFormat
error is related to which format I am using (and the respond_to
) to post to my controller, but even when I add a json
or html
format to the post - I still get the error.
Pulling my hair out over this test - would love some guidance on how best to rewrite this test for the best outcome.
The test
test "should create captable" do
sign_in @user
post company_captables_url(@company, @captable), params: { captable: { company_id: @captable.company_id, name: "Great captable!", version: @captable.version } }, as: :json
assert_response :success
end
The method from the controller:
def create
@captable = @company.captables.new(captable_params)
respond_to do |format|
if @captable.save
format.html { redirect_to [@company, @captable], notice: 'Captable was successfully created.' }
format.json { render :show, status: :created, location: [@company, @captable] }
else
format.html { render :new }
format.json { render json: @captable.errors, status: :unprocessable_entity }
end
end
end
The full backtrace on the error :
Error:
CaptablesControllerTest#test_should_create_captable:
ActionController::UnknownFormat: ActionController::UnknownFormat
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/mime_responds.rb:205:in `respond_to'
/Users/jamespember/calmcap/app/controllers/captables_controller.rb:24:in `create'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/base.rb:194:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/rendering.rb:30:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/callbacks.rb:42:in `block in process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/callbacks.rb:132:in `run_callbacks'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/callbacks.rb:41:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/rescue.rb:22:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:168:in `block in instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications/instrumenter.rb:23:in `instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/notifications.rb:168:in `instrument'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/instrumentation.rb:32:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/activerecord-5.2.1/lib/active_record/railties/controller_runtime.rb:24:in `process_action'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/abstract_controller/base.rb:134:in `process'
/usr/local/lib/ruby/gems/2.5.0/gems/actionview-5.2.1/lib/action_view/rendering.rb:32:in `process'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal.rb:191:in `dispatch'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_controller/metal.rb:252:in `dispatch'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:34:in `serve'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/journey/router.rb:52:in `block in serve'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/journey/router.rb:35:in `each'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/journey/router.rb:35:in `serve'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/routing/route_set.rb:840:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/warden-1.2.7/lib/warden/manager.rb:36:in `block in call'
/usr/local/lib/ruby/gems/2.5.0/gems/warden-1.2.7/lib/warden/manager.rb:35:in `catch'
/usr/local/lib/ruby/gems/2.5.0/gems/warden-1.2.7/lib/warden/manager.rb:35:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/tempfile_reaper.rb:15:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/etag.rb:25:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/conditional_get.rb:38:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/head.rb:12:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/http/content_security_policy.rb:18:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:232:in `context'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/session/abstract/id.rb:226:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/cookies.rb:670:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/callbacks.rb:98:in `run_callbacks'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/callbacks.rb:26:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:38:in `call_app'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `block in call'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `block in tagged'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:28:in `tagged'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/tagged_logging.rb:71:in `tagged'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/rack/logger.rb:26:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/request_id.rb:27:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/method_override.rb:22:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/runtime.rb:22:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/activesupport-5.2.1/lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/executor.rb:14:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/middleware/static.rb:127:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-2.0.5/lib/rack/sendfile.rb:111:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/railties-5.2.1/lib/rails/engine.rb:524:in `call'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
/usr/local/lib/ruby/gems/2.5.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/testing/integration.rb:263:in `process'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/testing/integration.rb:24:in `post'
/usr/local/lib/ruby/gems/2.5.0/gems/actionpack-5.2.1/lib/action_dispatch/testing/integration.rb:350:in `block (2 levels) in <module:Runner>'
/Users/jamespember/calmcap/test/controllers/captables_controller_test.rb:39:in `block in <class:CaptablesControllerTest>'
You should change the test, because you're creating a new Captable entity. You don't need @captable variable here. And you don't need company_id
inside the params - it is already in the url.
test "should create captable" do
sign_in @user
assert_difference '@company.captables.count', 1 do # it is good idea to check it was really created
post company_captables_url(@company), params: { captable: { name: "Great captable!", version: 'version_name' } }, as: :json
end
assert_redirected_to company_captable_url(@company, Captable.last)
end