jeudi 26 novembre 2020

Testing assert_not; capture change of data at proper moment

The following test

 test "update user no kee" do
    sign_in users(:admin)
    @user = users(:end_consumer_no_kee)
    assert_nil(@user.kee)
    patch update_data_user_url(@user),  params: { user: { storage_bin: 'testing!', nation_id: 1 } }
    assert_not_nil(@user.kee) 

using the following fixture

end_consumer_no_kee:
  id: 8
  email: end_consumer_no_kee@mail.co
  admin: false
  nation_id: 1
  mobile_nation_id: 1
  mobile: 547832902
  login_name: end_consumer_no_kee@mail.co
  encrypted_password: <%= Devise::Encryptor.digest(User, 'password') %>

calls a method which runs properly in practice. There is also a puts to highlight the situation

puts @user.inspect
if @user.mobile.nil? || @user.mobile_nation_id.nil?
  calc_twilio_number = nil
else
  nation = Nation.where(id: @user.mobile_nation_id).first
  calc_twilio_number = '+' + nation.phone_cc.to_s + @user.mobile.to_s
end
if @user.kee.nil?
  generated_kee = SecureRandom.alphanumeric(32)
  composed_virtual_qr_code = @user.login_name + generated_kee
  @user.update(kee: generated_kee, virtual_qr_code: composed_virtual_qr_code, twilio_number: calc_twilio_number)
else
  composed_virtual_qr_code = @user.login_name + @user.kee
  @user.update(virtual_qr_code: composed_virtual_qr_code, twilio_number: calc_twilio_number)
end
puts @user.inspect

which shows that the action is processed:

#<User id: 8, email: "end_consumer_no_kee@mail.co", mobile: 547832902, mobile_nation_id: 1, kee: nil  [...]
#<User id: 8, email: "end_consumer_no_kee@mail.co", mobile: 547832902, mobile_nation_id: 1, kee: "YMIWrIt4a9jNV7E49wKKaVyrQml6XSR7", virtual_qr_code: "end_consumer_no_kee@mail.coYMIWrIt4a9jNV7E49wKKaVy...", twilio_number: "+9547832902" [...]

Yet the test leads to a single failure

Failure:
ApplicationControllerTest#test_update_user_no_kee [/Volumes/SanJuanT/r/geodata/test/controllers/application_controller_test.rb:83]:
Expected nil to not be nil.

Thus, if the test runs properly in practice, but in testing is not catching the state of the attribute at the proper moment, the test is wrong. Where?

Aucun commentaire:

Enregistrer un commentaire