gpt4 book ai didi

ruby-on-rails - Rspec 测试搜查总是返回所有实例

转载 作者:行者123 更新时间:2023-12-04 07:40:28 24 4
gpt4 key购买 nike

我想使用 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

我的 Controller :

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

我做错了什么?

最佳答案

参数 q: 应该是这样的

q: {email_cont: '2'}

关于ruby-on-rails - Rspec 测试搜查总是返回所有实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30438079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com