Pregunta

I am new to Rails and have started working on a new project. But i am unable to find the exact solution for my Update controller this is my update controller.

 def update
    respond_to do |format|
      if @wallet.update(wallet_params)
        format.html { redirect_to @wallet, notice: 'Wallet was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @wallet.errors, status: :unprocessable_entity }
      end
    end
  end

In my wallets table i have id,user_id,balance,name. I have tried

   describe "PUT #update" do
     it "should update the wallet" do
        put :update, id :@wallet.id :wallet{ :name => "xyz", :balance => "20.2"}
     end
   end

Have even tried few things RSpec test PUT update action and How to write an RSpec test for a simple PUT update? but still not able to solve the problem.

¿Fue útil?

Solución

If you're using Rails 4, use PATCH instead of PUT; PUT still works, but PATCH is now preferred. To test that, try this:

describe "PATCH #update" do
  context "with good data" do
    it "updates the wallet and redirects" do
      patch :update, id: @wallet.id, wallet: { name: "xyz", balance: "20.2"}
      expect(response).to be_redirect
    end
  end
  context "with bad data" do
    it "does not change the wallet, and re-renders the form" do
      patch :update, id: @wallet.id, wallet: { name: "xyz", balance: "two"}
      expect(response).not_to be_redirect
    end
  end
end

You can make the expect clauses more specific, but that's a start. If you want to test the json part of the code, just add format: 'json' to the params hash.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top