RSpec和Factory出现意外测试失败

时间:2012-05-30 12:52:39

标签: ruby-on-rails ruby rspec factory

RoR控制器:

def destroy 
    Page.find(params[:id]).destroy 
    @pagechildren =  Page.where("parent_id = ?", params[:id]) 
    @pagechildren.update_all({parent_id: -1}) if @pagechildren.count > 
0 
    flash[:success] = 
t('activerecord.errors.controllers.message.attributes.page.page_destroy_suc cess') 
    redirect_to pages_path 
  end 

测试RSpec

     it "should remove parent_id from children" do 
       @page2 = Factory(:page) 
       @pagechild = Factory(:page) 
       @pagechild[:parent_id] = @page2.id 
       lambda do 
          delete :destroy, :id => @page2 
        end.should 
change(@pagechild, :parent_id).from(@page2.id).to(-1) 
      end 

代码正常工作(),但

Кодвсекорректнообрабатывает(抓住'id'删除页面,并为所有人 children parent_id更改为-1)。但是rspec测试失败了。

厂:

Factory.define :page do |page| 
  page.name "Name example page" 
  page.title "Title example page" 
  page.content "Content example page" 
  page.metadescription "metadescription example page" 
  page.metakeywords "metakeywords example page" 
  page.head "head example page" 
  page.ismenu true 
  page.order_id -1 
  page.parent_id -1 
end 

测试是红色=( 怎么了?

2 个答案:

答案 0 :(得分:2)

您的代码将更新数据库中的所有子项。但不是你的测试实例:) 试试这个:

it "should remove parent_id from children" do 
       @page2 = Factory.create(:page) 
       @pagechild = Factory.create(:page) 
       @pagechild[:parent_id] = @page2.id 
       delete :destroy, :id => @page2 
       @pagechild.reload.parent_id.should == -1
end

答案 1 :(得分:0)

尝试

delete :destroy, :id => @page2.id

并显示rspec输出,如果它仍然不起作用。