Danko
Danko

Reputation: 69

Unexpected test failure with RSpec and Factory

RoR Controller:

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 

Test 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 

The code works correctly (), but

Код все корректно обрабатывает (catches 'id' delete pages, and for all children parent_id changes to -1). But rspec test is fail.

Factory:

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 

test is red =( what's wrong?

Upvotes: 1

Views: 280

Answers (2)

Viktor Trón
Viktor Trón

Reputation: 8894

your code will update all children in the db. but not your test instance :) try this:

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

Upvotes: 2

Alexander
Alexander

Reputation: 1329

Try

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

and show rspec output if it still doesn't work.

Upvotes: 0

Related Questions