I have user model, query helper and a controller : the helper contain a method that return a list of user when send a search query request from the view:
class UserQueryHelper
def initialize( query = nil )
@query = Arel.sql("UNACCENT('%#{ query }%')")
end
def search
User.where( first_name.matches( @query ).or( last_name.matches( @query ) ))
end
protected
def users ; User.arel_table ; end
def first_name
Arel::Nodes::NamedFunction.new( "UNACCENT", [ users[ :first_name ] ] )
end
def last_name
Arel::Nodes::NamedFunction.new( "UNACCENT", [ users[ :last_name ] ] )
end
end
in the model I call that helper :
def self.search( query )
if query.present?
UserQueryHelper.new( query ).search
else
all
end
end
and in the controller I get the list of user searched by the admin :
users_to_export = User.regular.search(params[:search]).includes( :company )
I want to write some unit test for this and I didn't got the right way to do it and I tried this code but it doesn't work :
require 'spec_helper'
describe UserQueryHelper do
let!( :query ) { UserQueryHelper.new( "sahnoun" ) }
it "return user" do
user1 = create :user, first_name: "sahnoun", last_name: "mabrouk", company: $company
expect( query.send( :search ) ).to include user1
end
end