我想使用ransack做一个特定的搜索,但我的测试总是返回所有实例。
我的测试:
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'}