RSpec看不到模型毁灭

时间:2012-03-03 22:36:01

标签: rspec-rails ruby-on-rails-3.2

当我从控制器调用destroy时,RSpec没有看到模型的破坏。在ProjectsController#destroy方法中是否需要一些额外的命令来使rails实际执行destroy?或者告诉RSpec在销毁完成后重新加载项目计数? (Rails 3.2.2)

class ProjectsController < ApplicationController
  def destroy
    @project = current_user.my_projects.find params[:id]
    @project.destroy
  end
end

describe ProjectsController do
  let(:current_user) { Factory(:user) }
  let(:current_user_project) { current_user.my_projects.create(Factory.attributes_for(:project, owner: nil)) }

  context "one of the current users projects" do
    before { delete :destroy, format: :json, id: current_user_project }
    it { response.should be_success }
    it { expect { delete :destroy, format: :json, id: current_user_project }.to change(Project, :count).by(-1) }
  end
end

1 个答案:

答案 0 :(得分:1)

尽管我记得,在Rails中调用destroy只会删除数据库中的记录,只会冻结你的对象,这意味着你不能再分配值了。但它实际上并没有删除你的对象。因此,您必须重新加载以查看更改。

相关问题