Reputation: 41
My problem is I am trying to get as much coverage over my methods using rspec and I am unable to test a few certain lines. I am trying to pass a params hash to my controller method in my rspec to simulate the values from the view. Essentially, these values will filter results to be displayed on my index page.
The controller method I am testing is:
def index
@buildings = Building.all
@buildings = @buildings.searchaddress(params[:searchaddress])
if params[:searchcompany] != nil
@buildings = @buildings.searchcompany(params[:searchcompany][:management])
end
if params[:searchcompany] != nil
@buildings = @buildings.searchcity(params[:searchcity][:city])
end
if params[:searchparking] == 'on'
params[:searchparking] = 't'
@buildings = @buildings.searchparking(params[:searchparking])
end
if params[:searchpets] != nil
params[:searchpets] = 't'
@buildings = @buildings.searchpets(params[:searchpets])
end
end
I am trying to pass the params hash in my rspec test. I have tried a few ways including this one:
describe "viewing all buildings" do
it "renders index template" do
param = Hash.new()
param[:searchcompany] = [management:'asdf']
param[:searchcity] = [city:'asdf'] #have also tried {city:""}
param[:searchparking] = ['on']
param[:searchpets] = [true]
param[:searchaddress] = ['lalala']
get :index, params:param #{searchcompany:{management:'asdf'}, searchcity:{city:'asdf'}, searchparking:'on', searchpets:true}
expect(response).to render_template('index')
expect(assigns(:buildings)).to be_truthy
expect(Building).to receive(Building.searchcompany)
expect(Building).to receive(Building.searchcity)
expect(Building).to receive(Building.searchpets)
expect(Building).to receive(Building.searchparking)
end
end
The searchpets, searchcompany, etc. methods are from my Building model and are implemented as
def self.searchaddress(search)
where("address LIKE ?", "%#{search}%")
end
Here is the error I am getting:
1) BuildingsController viewing all buildings renders index template
Failure/Error:
def self.searchcity(search)
where("city LIKE ?", "%#{search}%")
ArgumentError:
wrong number of arguments (0 for 1)
# ./app/models/building.rb:39:in `searchcity'
# ./spec/controllers/buildings_controller_spec.rb:103:in `block (3 levels) in <top (required)>'
How do i pass [:searchcity][:city] to my controller method through my rspec test?
Upvotes: 2
Views: 4305
Reputation: 2166
You could try:
it "renders index template" do
params = {
searchcompany: {
management: 'asdf'
},
searchcity: {
city: 'asdf'
},
searchparking: 'on'
}
# All your expectations like 'expect(sth).to receive(:method)' go here
get :index, params
# Your remaining expectations go here
end
Upvotes: 2