控制器不会更改数据库记录计数

时间:2017-08-15 11:08:19

标签: ruby-on-rails ruby-on-rails-4 rspec rspec-rails rspec3

我尝试使用rspec向我的控制器编写测试。现在我有这样的代码:

class Restream::CustomsController < Restream::BaseController
  def create
    @custom = Restream::Custom.new(custom_params)
    @custom.user_id = current_user.id
    @custom.restream = current_user.restreams.find(params[:restream])

    if @custom.save
      @custom.restream.update_attributes!(provider: @custom)
      redirect_after_create
    else
      render 'edit'
    end
  end

  private
  def custom_params
    attributes = params.require(:restream_custom).permit(
      :server_url,
      :key,
      :admin_url,
      :view_url
    )

    if attributes[:view_url].present?
      attributes[:additional_parameters] = { view_url: attributes[:view_url] }
      attributes.delete(:view_url)
    end

    if attributes[:admin_url].present?
      if attributes[:additional_parameters].present?
        attributes[:additional_parameters].merge!({ manage_url: attributes[:admin_url] })
      else
        attributes[:additional_parameters] = { manage_url: attributes[:admin_url] }
      end
      attributes.delete(:admin_url)
    end

    attributes
  end

  def redirect_after_create
    if params[:redirect].present?
      redirect_to params[:redirect]
    else
      redirect_to restreams_path
    end
  end
end

所以,我尝试测试:create这个动作。 这是规范:

require 'rails_helper'

describe Restream::CustomsController do
  let(:restream)  { create(:restream) }
  let(:user)      { restream.user }

  before do
    login(user)
  end
  describe '#create' do
    let(:params_hash) { {
      'server_url' => "rtmp://example.com",
      'key' =>        "somekey",
      'admin_url' =>  "http://example.com/admin_url",
      'view_url'  =>  "http://example.com/view_url"
      } }
    it 'creates provider if params correct' do
      puts Restream::Custom.find_each.count
      expect {
        post :create, { :restream_custom => params_hash, :restream => restream.id }
        }.to change(Restream::Custom, :count).by 1
        # Restream::Custom.create(params_hash.merge(:user => user, :restream => restream))
      puts Restream::Custom.find_each.count

      expect(response).to redirect_to restreams_path
    end
  end
end

该测试失败。并在两个调试打印中打印00。 我确信控制器可以工作并且应该创建一个新记录。 另外,我有一个数据库输出,这让我陷入困境:

D, [2017-08-15T10:38:46.807264 #1] DEBUG -- :   SQL (1.1ms)  INSERT INTO "restream_services" ("type", "server_url", "key", "additional_parameters", "user_id", "restream_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id"  [["type", "custom"], ["server_url", "rtmp://example.com"], ["key", "somekey"], ["additional_parameters", "{\"view_url\"=>\"http://example.com/view_url\", \"manage_url\"=>\"http://example.com/admin_url\"}"], ["user_id", 1], ["restream_id", 1], ["created_at", "2017-08-15 10:38:46.803307"], ["updated_at", "2017-08-15 10:38:46.803307"]]
D, [2017-08-15T10:38:46.810992 #1] DEBUG -- :    (0.4ms)  RELEASE SAVEPOINT active_record_1
D, [2017-08-15T10:38:46.811829 #1] DEBUG -- :    (0.3ms)  SAVEPOINT active_record_1
D, [2017-08-15T10:38:46.815250 #1] DEBUG -- :   Restream::Service Load (0.7ms)  SELECT  "restream_services".* FROM "restream_services" WHERE "restream_services"."restream_id" = $1 LIMIT 1  [["restream_id", 1]]
D, [2017-08-15T10:38:46.818478 #1] DEBUG -- :   SQL (0.6ms)  DELETE FROM "restream_services" WHERE "restream_services"."id" = $1  [["id", 1]]
D, [2017-08-15T10:38:46.821715 #1] DEBUG -- :   Restream::Service Exists (1.0ms)  SELECT  1 AS one FROM "restream_services" WHERE ("restream_services"."restream_id" = 1 AND "restream_services"."id" != 1) LIMIT 1
D, [2017-08-15T10:38:46.827232 #1] DEBUG -- :    (0.5ms)  RELEASE SAVEPOINT active_record_1
I, [2017-08-15T10:38:46.828039 #1]  INFO -- : Redirected to http://test.host/restreams
I, [2017-08-15T10:38:46.828454 #1]  INFO -- : Completed 302 Found in 51ms (ActiveRecord: 7.8ms)
D, [2017-08-15T10:38:46.831293 #1] DEBUG -- :   Restream::Custom Load (1.0ms)  SELECT  "restream_services".* FROM "restream_services" WHERE "restream_services"."type" IN ('custom', 'periscope', 'odnoklassniki', 'vkontakte')  ORDER BY "restream_services"."id" ASC LIMIT 1000
D, [2017-08-15T10:38:46.834181 #1] DEBUG -- :    (0.6ms)  ROLLBACK

因此,它创建了一条记录,但几乎立即将其删除。

当我对第一个expect广告进行评论并使用Restream::Custom.create注释掉该字符串时,它会正常工作并且count会更改。

为什么会发生这种情况,我该怎么办呢?

1 个答案:

答案 0 :(得分:0)

摆脱界限 Restream::Custom.create(params_hash.merge(:user => user, :restream => restream)) 它应该工作。您不需要在测试中致电create。该记录是通过对:create控制器操作的请求创建的。测试的重点是模拟对该操作的请求并测试请求后发生的事情。