mardi 24 novembre 2020

Rails testing actions based on uniqueness of three joined attributes

A model has three references, which combined should be unique

class Roleshopuser < ApplicationRecord
  belongs_to :user
  belongs_to :shop
  belongs_to :role
  
  validates :user_id, uniqueness: { scope: [:shop_id, :role_id] }
end

However when testing a controller update action

puts @roleshopuser.role_id
puts (@roleshopuser.role_id + 1)
puts @roleshopuser.shop_id
puts @roleshopuser.user_id
    patch roleshopuser_url(@roleshopuser), params: { roleshopuser: { role_id: (@roleshopuser.role_id.to_i + 1), shop_id: @roleshopuser.shop_id, user_id: @roleshopuser.user_id } }
    assert_redirected_to roleshopuser_url(@roleshopuser)

the output of the IDs indicates proper data

689480364
689480365
459312771
483292939

Yet the result is a failure as the controller states if @roleshopuser.update(roleshopuser_params) format.html { redirect_to @roleshopuser [...] else format.html { render :edit }

Expected response to be a <3XX: redirect>, but was a <200: OK> 

and thus the record is not saving

What am I missing? is it something related to the join on three attirbutes?

Aucun commentaire:

Enregistrer un commentaire