2 years ago

#30121

test-img

Jerome

rails minitest assertions failing when contorller logging indicates success

this test runs a methods that nullifies an input (discount) in case two are provided.

patch promotion_url(promotions(:valid)), params: { promotion: { discount_percentage: 20, discount: 9.99 } }
assert_equal(20.00, promotions(:valid).reload.discount_percentage)
assert_nil promotions(:valid).reload.discount

The controller intercepts the data before and after the update action

puts params
    @promo.update(discount_percentage: params[:promotion][:discount_percentage], discount: params[:promotion][:discount])
puts @promo.discount_percentage
puts @promo.discount

and shows data as epxected

{"promotion"=><ActionController::Parameters {"discount_percentage"=>0.2e0, "discount"=>nil} permitted: false>, "controller"=>"promotions", "action"=>"update", "id"=>"1040"}
0.2
          # a blank line

but the test is obviously written badly as a failure is hit upon the first assertion:

Failure:
PromotionsControllerTest#test_should_update_promotion [...]:
Expected: 20.0
  Actual: nil

the result is the same without reload.

How should these be written?

ruby-on-rails

minitest

0 Answers

Your Answer

Accepted video resources