Rspec 测试搜查总是返回所有实例

Rspec testing ransack always returning all instances

我想使用 ransack 进行特定搜索,但我的测试总是 returns 所有实例。

我的测试:

RSpec.describe UsersController, type: :controller do

describe "GET #index" do

context 'ransack search by email' do

  let!(:user1) { create(:user, email: 'user1@example.com') }
  let!(:user2) { create(:user, email: 'user2@example.com') }

  context 'finds specific user' do
    before { get :index, q: '2' }

    it "should find just one user" do
      expect(assigns(:users).first).to eq [user2]
    end

    it { should respond_with(:success) }
    it { should render_template(:index) }
  end

我的控制器:

class UsersController < ApplicationController
  def index
    @q ||= User.ransack(params[:q])
    @users = @q.result(distinct: true)
  end
end

我做错了什么?

参数 q: 应该像

q: {email_cont: '2'}