Module: ValidAttribute::Method

Included in:
MiniTest::Spec, RSpec::Matchers, Test::Unit::TestCase
Defined in:
lib/valid_attribute/method.rb

Instance Method Summary collapse

Instance Method Details

#have_valid(attr) ⇒ ValidAttribute::Matcher

Assert an attribute is valid with any number of test values

examples:

describe User do
  it { should have_valid(:name).when('Brian') }
  it { should_not have_valid(:name).when(nil) }

  # if we want to test uniqueness we need to setup an existing value
  context 'email' do
    before { User.create(:email => '[email protected]') }
    it { should have_valid(:email).when('[email protected]', '[email protected]') }
    it { should_not have_valid(:email).when('abc', 123, '[email protected]') }
  end
end

If you are using Test::Unit you should use Thoughtbot‘s shoulda-context:

class UserTest < Test::Unit::TestCase
  should have_valid(:name).when('Brian')
  should_not have_valid(:name).when('nil')

  # if we want to test uniqueness we need to setup an existing value
  context 'email' do
    setup { User.create(:email => '[email protected]') }
    should have_valid(:email).when('[email protected]', '[email protected]')
    should_not have_valid(:email).when('abc', 123, '[email protected]')
  end
end

Parameters:

  • (Symbol)

Returns:



34
35
36
# File 'lib/valid_attribute/method.rb', line 34

def have_valid(attr)
  ::ValidAttribute::Matcher.new(attr)
end