Where am I going wrong?
describe '#update' do
let(:new_name) { Faker::Lorem.word }
let(:request) { patch :update, id: parent_folder.id, folder: { name: new_name, parent_id: nil, user_id: user.id } }
it 'should change the name' do
expect{ request }.to change(parent_folder.reload,:name).from(parent_folder.name).to(new_name)
end
it 'does work gadnammit' do
ap parent_folder.reload.name #=> e.g. aqua
request
ap parent_folder.reload.name #=> e.g. hortis
end
end
The result of 'should change the name' is
Failure/Error: expect{ request }.to change(parent_folder.reload,:name).from(parent_folder.name).to(new_name)# @new_name
expected #name to have changed from "maxime" to "jimmies", but did not change
And yet 'does work gadnammit' logs two different names:
All examples were filtered out; ignoring {:focus=>true}
F."omnis"
"jimmies"
Failures:
1) FoldersController#update should change the name
Failure/Error: expect{ request }.to change(parent_folder.reload,:name).from(parent_folder.name).to(new_name)# @new_name
expected #name to have changed from "maxime" to "jimmies", but did not change
# ./spec/controllers/folders_controller_spec.rb:62:in `block (3 levels) in <top (required)>'
Also, when you're using the let helper with Faker, I am aware you get a new result each time the variable it creates is called, which could be giving the illusion the name is changing in the 'does work gadnammit' spec. However, changing it to a hardcoded string yields no difference:
let(:new_name) { "stagnant" }
Also, this spec:
it 'should make database queries' do
expect{ request }.to make_database_queries
end
passes so I really think it is working...
SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
SELECT "folders".* FROM "folders" WHERE "folders"."user_id" = ? AND "folders"."id" = ? LIMIT 1
SAVEPOINT active_record_1
UPDATE "folders" SET "name" = ?, "updated_at" = ? WHERE "folders"."id" = 1
RELEASE SAVEPOINT active_record_1
(make_database_queries matcher courtesy of db-query-matchers gem)
TL;DR Why isn't
expect{ request }.to change(parent_folder.reload,:name).from(parent_folder.name).to(new_name)
working?