Project

General

Profile

« Previous | Next » 

Revision d6c08a7a

Added by Daniel Lobato Garcia over 8 years ago

Fixes #11618 - Replace validation tests by shoulda-matchers

A good chunk of our unit tests are testing whether a validation is
working or not by testing it actively. For the validations we've
added ourselves I would say it's fine. However for validations that
come from the Rails framework, we're essentially testing their job.

Instead of testing (for instance) validates(:name, :uniqueness =>
true) by creating two objects and verifying the second one won't
save (these tests are already done at the framework level), we
should simply test we're including validations in our models.

The well-maintained gem shoulda-matchers provide easy functions to
check we're including such validations, and other helpers we can
use to refactor our tests further and not test Rails functionality
twice.

View differences:

test/unit/hostgroup_test.rb
setup do
User.current = users :admin
end
test "name can't be blank" do
host_group = Hostgroup.new :name => " "
assert host_group.name.strip.empty?
assert !host_group.save
end
should validate_presence_of(:name)
should validate_uniqueness_of(:name)
should allow_value(nil).for(:root_pass)
should validate_length_of(:root_pass).is_at_least(8).
with_message('should be 8 characters or more')
test "name strips leading and trailing white spaces" do
host_group = Hostgroup.new :name => " all hosts in the world "
......
refute host_group.name.starts_with?(' ')
end
test "name must be unique" do
host_group = Hostgroup.new :name => "some hosts"
assert host_group.save
other_host_group = Hostgroup.new :name => "some hosts"
assert !other_host_group.save
end
def setup_user(operation)
super operation, "hostgroups"
end
test "should be able to nest a group parameters" do
# creates a 3 level hirecy, each one with his own parameters
# and overrides.
......
assert_equal (hostgroup.environment.puppetclasses - hostgroup.parent_classes).sort, hostgroup.available_puppetclasses.sort
end
test "hostgroup root pass can be blank" do
hostgroup = FactoryGirl.create(:hostgroup)
hostgroup.root_pass = nil
assert hostgroup.valid?
end
test "hostgroup root pass be must at least 8 characters if not blank" do
hostgroup = FactoryGirl.build(:hostgroup)
assert hostgroup.valid?
hostgroup.root_pass = '1234567'
refute hostgroup.valid?
assert_equal "should be 8 characters or more", hostgroup.errors[:root_pass].first
hostgroup.root_pass = '12345678'
assert hostgroup.valid?
end
test "root_pass inherited from parent if blank" do
parent = FactoryGirl.create(:hostgroup, :root_pass => '12345678')
hostgroup = FactoryGirl.build(:hostgroup, :parent => parent, :root_pass => '')
......
FactoryGirl.create_list(:host, 4, :managed, :hostgroup => nested_group )
assert_equal(7, group.parent.children_hosts_count)
end
private
def setup_user(operation)
super operation, "hostgroups"
end
end

Also available in: Unified diff