I have a model entities with a HMT relationship in the model user_entity_roles. I create new entities with a nested form. In the controller, this is a 2 step process via the new and create methods. I can’t figure out how to write a test for creating a new entity.
Models:
<code>class Entity < ApplicationRecord
has_many :invoices, dependent: :restrict_with_error
has_many :user_entity_roles, dependent: :destroy
accepts_nested_attributes_for :user_entity_roles
has_many :users, through: :user_entity_roles, dependent: :restrict_with_error
belongs_to :invoice_number_format
has_many :debitors, dependent: :restrict_with_error
validates_associated :user_entity_roles
validates :company, presence: true
validates :street, presence: true
validates :house_number, presence: true
validates :zip, presence: true
validates :city, presence: true
end
class UserEntityRole < ApplicationRecord
belongs_to :entity, touch: true
belongs_to :user
validates :entity_id, uniqueness: { scope: :user_id }
end
</code>
<code>class Entity < ApplicationRecord
has_many :invoices, dependent: :restrict_with_error
has_many :user_entity_roles, dependent: :destroy
accepts_nested_attributes_for :user_entity_roles
has_many :users, through: :user_entity_roles, dependent: :restrict_with_error
belongs_to :invoice_number_format
has_many :debitors, dependent: :restrict_with_error
validates_associated :user_entity_roles
validates :company, presence: true
validates :street, presence: true
validates :house_number, presence: true
validates :zip, presence: true
validates :city, presence: true
end
class UserEntityRole < ApplicationRecord
belongs_to :entity, touch: true
belongs_to :user
validates :entity_id, uniqueness: { scope: :user_id }
end
</code>
class Entity < ApplicationRecord
has_many :invoices, dependent: :restrict_with_error
has_many :user_entity_roles, dependent: :destroy
accepts_nested_attributes_for :user_entity_roles
has_many :users, through: :user_entity_roles, dependent: :restrict_with_error
belongs_to :invoice_number_format
has_many :debitors, dependent: :restrict_with_error
validates_associated :user_entity_roles
validates :company, presence: true
validates :street, presence: true
validates :house_number, presence: true
validates :zip, presence: true
validates :city, presence: true
end
class UserEntityRole < ApplicationRecord
belongs_to :entity, touch: true
belongs_to :user
validates :entity_id, uniqueness: { scope: :user_id }
end
Entities Controller:
<code>def new
@entity = Entity.new
@entity.user_entity_roles.build
@entity.user_entity_roles.first.user_id = Current.user.id
end
def create
@entity = Entity.new(entity_params)
if @entity.save
redirect_to @entity
else
render :new, status: :unprocessable_entity
end
end
</code>
<code>def new
@entity = Entity.new
@entity.user_entity_roles.build
@entity.user_entity_roles.first.user_id = Current.user.id
end
def create
@entity = Entity.new(entity_params)
if @entity.save
redirect_to @entity
else
render :new, status: :unprocessable_entity
end
end
</code>
def new
@entity = Entity.new
@entity.user_entity_roles.build
@entity.user_entity_roles.first.user_id = Current.user.id
end
def create
@entity = Entity.new(entity_params)
if @entity.save
redirect_to @entity
else
render :new, status: :unprocessable_entity
end
end
Entities Test Controller:
<code>test 'should create entity with valid params' do
sign_in_as @user
assert_difference -> { Entity.count }, 1 do
post entities_url, params: {
entity: {
company: 'MyString',
street: 'MyString',
house_number: 'MyString',
zip: 'MyString',
city: 'MyString',
tax_number: 'MyString',
vat_id: 'MyString',
hrb: 'MyString',
invoice_number_format_id: 2,
user_entity_roles_attributes: {
id: 0, # This is wrong
user_id: @user.id,
is_manager: 1,
is_editor: 1
}
}
}
end
end
</code>
<code>test 'should create entity with valid params' do
sign_in_as @user
assert_difference -> { Entity.count }, 1 do
post entities_url, params: {
entity: {
company: 'MyString',
street: 'MyString',
house_number: 'MyString',
zip: 'MyString',
city: 'MyString',
tax_number: 'MyString',
vat_id: 'MyString',
hrb: 'MyString',
invoice_number_format_id: 2,
user_entity_roles_attributes: {
id: 0, # This is wrong
user_id: @user.id,
is_manager: 1,
is_editor: 1
}
}
}
end
end
</code>
test 'should create entity with valid params' do
sign_in_as @user
assert_difference -> { Entity.count }, 1 do
post entities_url, params: {
entity: {
company: 'MyString',
street: 'MyString',
house_number: 'MyString',
zip: 'MyString',
city: 'MyString',
tax_number: 'MyString',
vat_id: 'MyString',
hrb: 'MyString',
invoice_number_format_id: 2,
user_entity_roles_attributes: {
id: 0, # This is wrong
user_id: @user.id,
is_manager: 1,
is_editor: 1
}
}
}
end
end
This doesn’t work, because the entity_id in the nested user_entity_role must be the id of the newly created entity.