dimanche 23 avril 2017

Rails 5: How do I test a form that has nested models of multiple layers?

I am trying to test my user_signup form that is 2 models deep.

user -> user_setting -> general_setting

test "should do the correct procedures when valid signup + activation" do
    get signup_path     
    assert_difference 'User.count', 1 do
        post signup_path, params: 
        {user: 
            {email: "test_user@gmail.com", password: "tester123",
             password_confirmation: "tester123",
             user_setting_attributes: 
                { general_setting_attributes: 
                    {first_name: "tester", last_name: "the tester",
                     birthday: Time.zone.now - 19.years, male: 1}}}}                                    
    end     
    assert_equal 1, ActionMailer::Base.deliveries.size

All I get with debugger is:

{:"user_setting.user"=>["must exist"], :"user_setting.general_setting.user_setting"=>["must exist"], :email=>[], :password=>[], :password_confirmation=>[]}

My general_setting.rb

class GeneralSetting < ApplicationRecord
  belongs_to :user_setting
end

user_setting.rb

class UserSetting < ApplicationRecord
  belongs_to :user
  has_one :general_setting
end

user.rb

class User < ApplicationRecord
  has_one :user_setting
   has_one :general_detail, through: :user_setting, source: :general_setting
  accepts_nested_attributes_for :user_setting
  validates :user_setting, presence: true
end

Aucun commentaire:

Enregistrer un commentaire