预期阻止并不像我期望的那样工作。我有一个期望,should change the name
:
let!(:user) { FactoryGirl.create(:user) }
let!(:parent_folder) { FactoryGirl.create(:folder, user_id: user.id) }
before { sign_in user }
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, :name).from(parent_folder.name).to(new_name)
end
end
结果是:
2) FoldersController#update should change the name
Failure/Error: expect {
expected #name to have changed from "consequatur" to "molestias", but did not change
这是为什么?我向你保证会发生变化:
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
更新
我的更新操作:
def update
respond_to do |format|
if @folder.update(folder_params)
format.html { redirect_to @folder, notice: 'Folder was successfully updated.' }
format.json { render :show, status: :ok, location: @folder }
else
format.html { render :edit }
format.json { render json: @folder.errors, status: :unprocessable_entity }
end
end
end
当我在浏览器中自己动手时,效果很好......
答案 0 :(得分:3)
问题是您的parent_folder对象需要重新加载才能看到更改。更改您使用parent_folder.reload.name
的期望,而不是这样做:
it 'should change the name' do
expect {
request
}.to change{ parent_folder.reload.name }.from(parent_folder.name).to(new_name)
end