简体   繁体   English

RSPEC未通过控制器测试(事务)但在浏览器中工作(rails 4)

[英]RSPEC fails controller test (transaction) but works in the browser (rails 4)

I am having a strange issue with RSPEC that I am unable to nail down. 我与RSPEC有一个奇怪的问题,我无法确定。 The test fails but when I try in the browser the behavior works as expected. 测试失败,但是当我在浏览器中尝试时,行为按预期工作。

Here is the model code so far: 这是目前的型号代码:

class QueueItem < ActiveRecord::Base
  belongs_to :video
  belongs_to :user

  validates_presence_of :user_id, :video_id
  validates_uniqueness_of :video_id, scope: :user_id
  validates_numericality_of :position, only_integer: true

  delegate :category, to: :video
  delegate :title, to: :video, prefix: :video

...

end

And here is the controller code so far: 这是目前为止的控制器代码:

class QueueItemsController < ApplicationController

  ...

  before_action :require_user

  def update_queue
    begin
      ActiveRecord::Base.transaction do
        queue_items_params.each do |queue_item_input|
          queue_item = QueueItem.find(queue_item_input[:id])
          queue_item.update!(position: queue_item_input[:position])
        end
      end
    rescue ActiveRecord::RecordInvalid
      flash[:danger] = "Your queue was not updated, make sure you only use numbers to set the position"
      redirect_to queue_items_path
      return
    end
    current_user.queue_items.each_with_index { |queue_item, i| queue_item.update(position: i+1 )}
    redirect_to queue_items_path

  end


  private

  def queue_items_params
    params.permit(queue_items:[:id, :position])[:queue_items]
  end

  ...
end

And now the controller spec that fails: 现在控制器规格失败了:

  describe "POST #update" do
    context 'when user is signed in' do
      let(:user) { Fabricate(:user) }
      before { session[:user] = user.id }

      context 'with invalid attributes' do
        it "does not update the queue items position" do
          queue_item1 = Fabricate(:queue_item, user: user, position: 1)
          queue_item2 = Fabricate(:queue_item, user: user, position: 2)
          post :update_queue, queue_items: [{id: queue_item1.id, position: 6}, {id: queue_item2.id, position: 2.2}]
          expect(queue_item1.reload.position).to  eq(1)
          expect(queue_item2.reload.position).to  eq(2)
        end
      end

    end
  end

And the error message: 并且错误消息:

Failures: 失败:

  1) QueueItemsController POST #update when user is signed in with invalid attributes does not update the queue items position
     Failure/Error: expect(queue_item1.reload.position).to  eq(1)

       expected: 1
            got: 6

       (compared using ==)

I don't understand why the spec fails but in the browser it works. 我不明白为什么规范失败但在浏览器中它工作。 What am I missing? 我错过了什么?

Thank you very much 非常感谢你

I actually found the issue! 我实际上发现了这个问题! It was due to DatabaseCleaner gem which I am using in my rspec_helper.rb setup. 这是由于我在我的rspec_helper.rb设置中使用的DatabaseCleaner gem。

The following setup did not work: 以下设置不起作用:

  config.before(:suite) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
  end

I fixed it by changing it to: 我修改它来修改它:

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :deletion
  end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM