When the below test is executed it fails on the last assert with <1>
expected but was <0>. How was save unsuccessful, but the count of the
associations afterward 0?
Thanks.
app/model/user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :recoverable, :rememberable,
:trackable, :validatable
attr_accessible :email, :password, :password_confirmation, :remember_me
has_many :organization_roles
has_many :roles, :through => :organization_roles
validates :organization_roles, :presence => true
accepts_nested_attributes_for :organization_roles
def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
end
test/unit/user_test.rb
require ‘test_helper’
class UserTest < ActiveSupport::TestCase
test “cannot remove all roles” do
user = users(:one)
assert_equal 1, user.organization_roles.count
user.organization_roles.clear
assert_false user.save
assert_false user.errors[:organization_roles].blank?
assert_equal 1, user.reload.organization_roles.count
end
end