I've got a User
model with validation when new user has an account_type set as 'organisation'
the company_name
should be mandatory.
class User < ApplicationRecord
ACCOUNT_TYPES = %w[individual organisation].freeze
validates :company_name, presence: true, if: :organisation_account?
private
def organisation_account?
account_type == ACCOUNT_TYPES[1]
end
end
I want to test this validation using Minitest and Shoulda Matchers, like below:
require 'test_helper'
class UserTest < ActiveSupport::TestCase
setup do
@user = users(:active)
end
context 'validations' do
context 'when account_type is organisation' do
@user.account_type = 'organisation'
should validate_presence_of(:company_name)
end
end
But the test show me an error: undefined method account_type=' for nil:NilClass (NoMethodError)
which means that for some reason the setup
block does not create a user. What did I missed?
What did I missed?
Your tests should be in an it
block. context
blocks merely set up test environment, they don't define tests.
And, since there's not a single test in this file, the setup
block didn't run.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.