this is controller
def update
respond_to do |format|
if @line_item.update(line_item_params)
format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
format.json { render :show, status: :ok, location: @line_item }
else
format.html { render :edit }
format.json { render json: @line_item.errors, status: :unprocessable_entity }
end
end
end
private
def line_item_params
params.require(:line_item).permit(:product_id, :cart_id) #Check this
end
Test case I have written in order to fail such that it renders edit
context "with invalid parameters" do
it "renders a successful response (i.e. to display the 'edit' template)" do
order = create(:order, user_id: user.id, email: user.email)
category = create(:category)
product = create(:product, category_id: category.id)
cart = create(:cart)
line_item = create(:line_item,order_id: order.id,product_id: product.id,cart_id:cart.id)
line_item.product_id = 1
patch line_item_url(line_item), params: { line_item: { product_id:line_item.product_id}}
expect(response).to render_template(:edit)
end
end
I am getting an error below:
Failure/Error: if @line_item.update(line_item_params)
ActiveRecord::InvalidForeignKey:
PG::ForeignKeyViolation: ERROR: insert or update on table "line_items" violates foreign key constraint "fk_rails_11e15d5c6b"
DETAIL: Key (product_id)=(1) is not present in table "products".
It is not going to the else part.
I Am not able to check with the invalid parameters. Please let me know Thank you
Well, every time you run this script, database is truncated or deleted (depends on your gem environment), that means that PK (primary key) for table every time incremented
++. Soline_item.product_id = 1it is totally invalid, cause 1 ID could be available during very first running of the test spec.Change to dynamic assignment, and you will get rid of that error.