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/functional/filters_controller_test.rb
require 'test_helper'
class FiltersControllerTest < ActionController::TestCase
basic_index_test('filters')
basic_new_test
basic_edit_test(Filter.first, 'filter')
setup do
User.current = users(:admin)
end
test 'get index' do
get :index, {}, set_session_user
assert_response :success
assert_template 'index'
assert_not_nil assigns(:filters)
end
test 'get new' do
get :new, {}, set_session_user
assert_response :success
assert_template 'new'
end
test "changes should expire topbar cache" do
user1 = FactoryGirl.create(:user, :with_mail)
user2 = FactoryGirl.create(:user, :with_mail)
......
assert_equal Filter.search_for("role = #{role.name}").count, assigns(:filters).count
end
test "should get new" do
get :new, {}, set_session_user
assert_response :success
end
test "should create filter" do
assert_difference('Filter.count') do
post :create, {:filter => { :role_id => roles(:manager).id, :permission_ids => [permissions(:access_dashboard).id] }}, set_session_user
......
assert_redirected_to filters_path
end
test "should get edit" do
get :edit, {:id => filters(:manager_1)}, set_session_user
assert_response :success
end
test "should update filter" do
put :update, {:id => filters(:manager_1), :filter => { :permission_ids => [permissions(:access_dashboard).id] }}, set_session_user
assert_redirected_to filters_path

Also available in: Unified diff