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/role_test.rb
require "test_helper"
class RoleTest < ActiveSupport::TestCase
it "should respond_to user_roles" do
role = roles(:manager)
role.must_respond_to :user_roles
role.must_respond_to :users
end
it "should have unique name" do
# Manager is in role fixtures
Role.new(:name => "Manager").wont_be :valid?
role = Role.new(:name => "Supervisor")
role.must_be :valid?
end
it "should not be valid without a name" do
role = Role.new(:name => "")
role.wont_be :valid?
end
it "should allow value 'a role name' for name" do
role = Role.new(:name => "a role name")
role.must_be :valid?
end
it "should allow utf characters in name" do
role = Role.new(:name => "トメル34;。")
role.must_be :valid?
end
it "should allow email address in name" do
role = Role.new(:name => "test@example.com")
role.must_be :valid?
end
should have_many(:user_roles)
should validate_presence_of(:name)
should validate_uniqueness_of(:name)
should allow_value('a role name').for(:name)
should allow_value('トメル34;。').for(:name)
should allow_value('test@example.com').for(:name)
it "should strip leading space on name" do
role = Role.new(:name => " a role name")

Also available in: Unified diff