1

救世主。

各 RSpec の例の後でデータベースをクリーニングするのに問題があります。問題は、コマンドを実行するとrspec、users_controller_spec.rb が、例が期待するよりも多くのレコードがあると不平を言うことです。で確認すると確かにレコードが作成されているようですrails c。このスイートを単独で実行すると成功するので、DatabaseCleaner が他の仕様が作成するユーザー レコードを消去しないためだと思います (ユーザー レコードの数は、users_controller_spec の例で主張されている余分なレコードと一致します)。それらはbefore :allブロックで作成されます(それが重要な場合)。

これが私のrails_helper.rb

# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'

# Add additional requires below this line. Rails is not loaded until this point!
require 'devise'
require 'admin/v1/dashboard_controller'
# Requires supporting ruby files with custom matchers and macros, etc, in
Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
  # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  config.fixture_path = "#{::Rails.root}/spec/fixtures"

  config.include Devise::Test::ControllerHelpers, type: :controller
  config.include ControllerMacros, type: :controller
  # If you're not using ActiveRecord, or you'd prefer not to run each of your
  # examples within a transaction, remove the following line or assign false
  # instead of true.
  config.use_transactional_fixtures = true

  config.include FactoryGirl::Syntax::Methods

  config.infer_spec_type_from_file_location!

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

  config.around(:each) do |example|
    DatabaseCleaner.cleaning do
      example.run
    end
  end
end

users_controller.rb

describe 'GET #index' do
  it 'populates an array of users' do
    user1 = create(:user)
    user2 = create(:user)
    get :index
    expect(assigns(:users)).to match_array([user1, user2])
  end
  it 'renders :index template' do
    get :index, {}
    expect(response).to render_template :index
  end
end

UPDATE1: ここで追加のuserレコードが作成されます

    require 'rails_helper'

describe Admin::V1::MessagesController do
  let(:admin_user) do
    admin_user = double('admin_user')
    allow(request.env['warden']).to receive(:authenticate!).and_return(admin_user)
    allow(controller).to receive(:current_admin_v1_admin_user).and_return(admin_user)
    p '==='
  end
  before { login_admin_user admin_user }

  describe 'GET #index' do
    it 'renders :index template' do
      get :index, {}
      expect(response).to render_template :index
    end
  end

  describe 'GET #get_users' do
    before :all do
      @user1 = create(:user, nickname: 'hiro')
      @user2 = create(:user, nickname: 'elise')
    end
    context 'with params' do
      it 'populates an array of users matching on nickname' do
        get :get_users, format: :json, query: 'h'
        expect(assigns(:users)).to match_array([@user1])
      end
    end
    context 'without params' do
      it 'populates an array of all users' do
        get :get_users, format: :json
        expect(assigns(:users)).to match_array([@user1, @user2])
      end
    end
  end

  describe 'GET #get_messages' do
    before :all do
      @user1 = create(:user)
      @user2 = create(:user)
      @message1 = create(:message, user_id: @user1.id)
      @message2 = create(:message, user_id: @user1.id)
      @message3 = create(:message, user_id: @user2.id)
    end
    context 'with user_id' do
      it 'populates an array of messages with the user_id' do
        get :get_messages, format: :json, user_id: @user1.id
        expect(assigns(:messages)).to match_array([@message1, @message2])
      end
    end
  end
end
4

1 に答える 1