New to RSpec and having trouble stubbing out an update method (needs to be stubbed) and am running into this error. I have tried a bunch of different ways and can't seem to solve it. I tried to include only the necessary code.
/spec/controllers/admin/business_controller_spec.rb
:
require 'spec_helper'
RSpec.describe Admin::BusinessesController, type: :controller do
render_views
before(:all) do
@business = build_stubbed(:business)
end
describe '#update' do
before(:each) do
allow(Business).to receive(:find).and_return(@business)
@new_name = Faker::Company.name
@params = { id: @business.id, business: { name: @new_name } }
end
it 'updates business name' do
expect(@business).to receive(:update_attributes).with(name: @new_name, confirm_flag: @business.confirm_flag, phone_primary: @business.phone_primary).and_return(true)
xhr :patch, :update, id: @business.id, business: { name: @new_name }
expect(@business.name).to eq(@new_name)
end
end
end
/app/controllers/admin/businesses_controller.rb
class Admin::BusinessesController < Admin::BaseAdminController
def update
@business.update_attributes(business_params)
end
def business_params
params.require(:business).permit(:name, :active_flag).merge(confirm_flag: true, phone_primary: '')
end
end
When I run this test I get this error:
Failures:
1) Admin::BusinessesController#update updates business name
Failure/Error: @business.update_attributes(business_params)
#<Business:0x007fc43c4f5738> received :update_attributes with unexpected arguments
expected: ({:name=>"Fadel, Larson and Hettinger", :confirm_flag=>true, :phone_primary=>"7832900175"})
got: ({"name"=>"Fadel, Larson and Hettinger", "confirm_flag"=>true, "phone_primary"=>""})
Diff:
@@ -1,4 +1,4 @@
-[{:name=>"Fadel, Larson and Hettinger",
- :confirm_flag=>true,
- :phone_primary=>"7832900175"}]
+[{"name"=>"Fadel, Larson and Hettinger",
+ "confirm_flag"=>true,
+ "phone_primary"=>""}]
So it looks like the phone_primary
is causing the problem and I change my test expectation line to be:
describe '#update' do
before(:each) do
allow(Business).to receive(:find).and_return(@business)
@new_name = Faker::Company.name
@params = { id: @business.id, business: { name: @new_name } }
end
it 'updates business name' do
expect(@business).to receive(:update_attributes).with(name: @new_name, confirm_flag: @business.confirm_flag, **phone_primary: ''**).and_return(true)
xhr :patch, :update, id: @business.id, business: { name: @new_name }
expect(@business.name).to eq(@new_name)
end
end
and i get the failure:
Failures:
1) Admin::BusinessesController#update updates business name
Failure/Error: expect(@business.name).to eq(@new_name)
expected: "Flatley Inc"
got: "Samara Kuhic"
And now the names aren't matching
.and_call_original
to your stubbing ofupdate_attributes
. Right it doesn't do any updating and your later expectation fails, naturally. – Sergio Tulentsev