2011-12-09 13 views
6

Ecco il codice rspec per il regolatore:Flash [: Avviso] .should_not be_nil fallito nel rspec

it "should render edit if update was not saved" do 
    item = Factory(:lease_item) 
    session[:corp_head] = true 
    post 'update', {:id => item.id, :name => 'nil'} 
    flash[:notice].should_not be_nil 
    response.should render 'edit'  
end 

L'aggiornamento controller è:

def update 
    if (eng? && dept_head?) || corp_head? || ceo? 
     @lease_item = LeaseItem.find(params[:id]) 
     @lease_item.input_by_id = session[:user_id] 
     if @lease_item.update_attributes(params[:lease_item], :as => :roles_update) 

     #redirect 
     redirect_to URI.escape("/view_handler?index=0&msg=Lease item updated successfully") 
     else 
     #back to new 
     render 'edit', :notice => "Item NOT updated" 
     end 
    else 
     #back to previous page 
     redirect_to URI.escape("/view_handler?index=0&msg=NO right to update lease item") 
    end  
    end 

Ecco il codice di errore da RSpec:

1) LeaseItemsController GET 'update' should render edit if update was not saved 
    Failure/Error: flash[:notice].should_not be_nil 
     expected: not nil 
      got: nil 

"L'elemento NON aggiornato" era previsto in flash. Tuttavia, perché non c'è niente con il flash [: avviso]? O come rspec c'è un messaggio con render 'edit',: notice => 'Item NOT updated'

Grazie.

UPDATE:

Qui è il cambiamento nel controllore:

 ........... 
     else 
     #back to new 
     flash[:notice] = "Item NOT updated" 
     render 'edit' 
     end 
     ......... 

Ecco il codice RSpec che passa:

it "should render edit if update was not saved" do 
     item = Factory(:lease_item) 
     session[:corp_head] = true 
     post 'update', {:id => item.id, :lease_item => {:name => 'nil'}} 
     flash.should_not be_nil 
     response.should render_template(:action=> "edit") 
    end 

non ha funzionato, se si usa il flash [: avviso ] .should_not be_nil (o .. flash.now [: notice] ...). L'errore è ottenuto nullo, che è lo stesso di prima. Anche response.should render 'edit' (o ... render: action => 'edit') non è passato bene. L'errore è NameError o NoMethodError. Non so perché.

+0

si sta inviando "elemento non aggiornato" nel 'notice' variabile al partial 'edit', ma stai effettivamente impostando 'flash [: notice]'? – Karl

+0

Come impostare il flash [: avviso]? – user938363

+0

non c'è rendering 'foo',: notice => "blah" ... leggi i documenti. Usa flash [: avviso] = "Articolo NON aggiornato", quindi visualizza "modifica" – daniel

risposta

5

cambiare la vostra altro a:

else 
    #back to new 
    flash[:notice] = "Item NOT updated" 
    render 'edit' 
end 
+0

rspec restituisce ancora errore: Errore/Errore: flash [: avviso] .should_not be_nil previsto: non nulla ottenuto: n. – user938363

+0

Davvero? do response.should render 'edit' e quindi flash [: notice] .should_not be_nil. – daniel

+0

L'errore potrebbe essere causato dalla visualizzazione "modifica" non ancora implementata ... – user938363

0

In primo luogo, non riesco a vedere dove esattamente si imposta il flash nel codice.

Qualunque cosa, sostituire:

post 'update', {:id => item.id, :name => 'nil'} 

con:

post 'update', {:id => item.id, :lease_item => { :name => 'nil' } } 
+0

: l'avviso è incluso nel comando di rendering. Finisce in flash, vero? – user938363

+0

Non c'è nessun errore con entrambi i comandi post. C'è una vera differenza tra questi due post? – user938363

+0

: avviso non è nel comando di rendering. Render non è nemmeno un comando ... guarda https://github.com/rails/rails/blob/d5fd83f4c7f01cc1761783408267cce4d5da1946/actionpack/lib/action_view/render/rendering.rb#L16 – daniel

Problemi correlati