Question

Je suis nouveau dans RSpec et Hérité_Resources. J'ai une ressource simple, contact, qui a un champ de nom. Le contrôleur n'a pas de fonctionnalité spéciale.

class ContactsController <  InheritedResources::Base 
  actions :all, :except => [:show]
end

J'ai écrit des spécifications pour créer et indexer très bien, en utilisant mock_model. Cependant, lors de l'utilisation de mock_model sur la mise à jour, il n'a pas pu trouver le contact lors de la mise en place. Donc, je suis passé à l'utilisation de modèles réels:

describe "PUT update" do
let(:contact) { Contact.create(:name => "test 1") }

it "edits the contact" do
  contact.name = "#{contact.name}-edited"
end
context "when the contact updates successfully" do
  before do
    contact.stub(:save).and_return(true)
  end
  it "redirects to the Contacts index" do
    put :update, :id => contact.id, :contact => contact
    #assigns[:contact].name = "test 1 - edited"
    response.should redirect_to(:action => "index")
  end
end

context "when the contact fails to save" do
  before do
    contact.stub(:save).and_return(false)
    contact.stub(:update_attributes).and_return(false)
    contact.stub(:errors).and_return(:errors => { :anything => "anything" })
  end
  it "renders the edit template" do
    put :update, :id => contact.id, :contact => contact
    response.should render_template :edit
  end
end
end

J'obtiens l'erreur suivante:

Failures:

  1) ContactsController PUT update when the contact fails to save renders the edit template
   Failure/Error: response.should render_template :edit
   Expected block to return true value.
   # ./spec/controllers/contacts_controller_spec.rb:82:in `block (4 levels) in <top (required)>'

Lorsque j'inspecte le statut_code, c'est une redirection 302 vers / contacts /: id.

Qu'est-ce que je fais mal?

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top