4
votes

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.

1
try this may be it will be helpful for you stackoverflow.com/questions/7060521/…Pulkit Agarwal
What's the problem. Do you get an error? Your test makes no assertion? What's happening? What do you expect to happen?Derek Prior

1 Answers

6
votes

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.