Ruby on Rails testing controller for nested form creation

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật