Rspec测试ransack总是返回所有实例

时间:2015-05-25 12:10:29

标签: ruby-on-rails rspec ransack

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

我做错了什么?

1 个答案:

答案 0 :(得分:0)

参数q:应该像

q: {email_cont: '2'}