class Object

Public Instance Methods

helper(name) click to toggle source
# File lib/minitest_rails_tools/helper_extension.rb, line 30
def helper(name)
  HelperExtension.new(name)
end
must_belong_to(association_name) click to toggle source

Allows for some automagic association tests.

# File lib/minitest_rails_tools/association_matchers.rb, line 5
def must_belong_to(association_name)
  it "belongs_to :#{association_name.to_s.parameterize.underscore}" do
    subject.association(association_name).must_be_kind_of(ActiveRecord::Associations::BelongsToAssociation)
    subject.send(association_name)
  end
end
must_differ(expression, difference = 1, message = nil, &block) click to toggle source
# File lib/minitest_rails_tools/expectations.rb, line 39
def must_differ(expression, difference = 1, message = nil, &block)
  metaclass = class << self; self end
  metaclass.send(:define_method, :callback)
  assert_difference("#{self}.#{expression}", difference, message, &block)
end
must_have_and_belong_to_many(association_name) click to toggle source
# File lib/minitest_rails_tools/association_matchers.rb, line 33
def must_have_and_belong_to_many(association_name)
  it "has_and_belongs_to_many :#{association_name.to_s.parameterize.underscore}" do
    subject.association(association_name).must_be_kind_of(ActiveRecord::Associations::HasAndBelongsToManyAssociation)
    subject.send(association_name).must_be_kind_of(Array)
  end
end
must_have_many(association_name) click to toggle source
# File lib/minitest_rails_tools/association_matchers.rb, line 19
def must_have_many(association_name)
  it "has_many :#{association_name.to_s.parameterize.underscore}" do
    subject.association(association_name).must_be_kind_of(ActiveRecord::Associations::HasManyAssociation)
    subject.send(association_name).must_be_kind_of(Array)
  end
end
must_have_many_through(association_name, association_name_through) click to toggle source
# File lib/minitest_rails_tools/association_matchers.rb, line 26
def must_have_many_through(association_name, association_name_through)
  it "has_many :#{association_name.to_s.parameterize.underscore} through :#{association_name_through.to_s.parameterize.underscore}" do
    subject.association(association_name).must_be_kind_of(ActiveRecord::Associations::HasManyThroughAssociation)
    subject.send(association_name).must_be_kind_of(Array)
  end
end
must_have_one(association_name) click to toggle source
# File lib/minitest_rails_tools/association_matchers.rb, line 12
def must_have_one(association_name)
  it "has_one :#{association_name.to_s.parameterize.underscore}" do
    subject.association(association_name).must_be_kind_of(ActiveRecord::Associations::HasOneAssociation)
    subject.send(association_name)
  end
end
must_validate_length_of(attribute, options = {}) click to toggle source
# File lib/minitest_rails_tools/validation_matchers.rb, line 19
def must_validate_length_of(attribute, options = {})
  if options[:minimum]
    spec(
      __method__, attribute, options.except(:maximum),
      "is too short (minimum is #{options[:minimum]}", (0..options[:minimum] - 1).to_a
    )
  end
  if options[:maximum]
    spec(
      __method__, attribute, options.except(:minimum),
      "is too long (maximum is #{options[:maximum]}", (0..options[:maximum] + 1).to_a
    )
  end
end
must_validate_presence_of(attribute, options = {}) click to toggle source

Allows for some automagic association tests. Expects 'subject' to be defined. E.g.

subject { FactoryGirl.create :foo }
# File lib/minitest_rails_tools/validation_matchers.rb, line 7
def must_validate_presence_of(attribute, options = {})
  spec(__method__, attribute, options, "can't be blank", nil)
end
must_validate_uniqueness_of(attribute, options = {}) click to toggle source
# File lib/minitest_rails_tools/validation_matchers.rb, line 11
def must_validate_uniqueness_of(attribute, options = {})
  spec(__method__, attribute, options, "has already been taken", :not_uniq) do
    other_instance = self.to_s.rstrip.constantize.new
    other_instance.send "#{attribute}=".to_sym, :not_uniq
    other_instance.save!(:validate => false)
  end
end
spec(name, attribute, options, default_message, value) { || ... } click to toggle source
# File lib/minitest_rails_tools/validation_matchers.rb, line 37
def spec(name, attribute, options, default_message, value)
  title = "#{name.to_s[4..-1]} :#{attribute.to_s.parameterize.underscore}"
  title += options.to_s[1..-2].gsub('=>', ' => ') unless options.blank?
  it title do
    yield if block_given?
    subject.send "#{attribute}=".to_sym, value
    subject.wont_be :valid?
    message = options[:message] || default_message
    subject.errors.messages[attribute.to_sym].must_include message
  end
end