0

我正在为我的一个控制器编写一些测试,除了我的 #create 方法的测试之外,所有这些测试都通过了。

我似乎无法通过这个测试。我知道该UnknownFormat错误与我使用哪种格式(和respond_to)发布到我的控制器有关,但即使我在帖子中添加jsonhtml格式 - 我仍然收到错误。

在这个测试中拔出我的头发 - 会喜欢一些关于如何最好地重写这个测试以获得最佳结果的指导。

考试

  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

来自控制器的方法:

  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

错误的完整回溯:

   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>'
4

1 回答 1

1

您应该更改测试,因为您正在创建一个新的 Captable 实体。您在这里不需要@captable 变量。而且您不需要company_id在参数内部 - 它已经在 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
于 2018-10-20T14:27:55.060 回答