【问题标题】:Rspec testing ransack always returning all instancesRspec测试洗劫总是返回所有实例
【发布时间】:2015-05-25 12:10:29
【问题描述】:

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

我做错了什么?

【问题讨论】:

    标签: ruby-on-rails rspec ransack


    【解决方案1】:

    参数 q: 应该是这样的

    q: {email_cont: '2'}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多