split up answer validation test into themed files

This commit is contained in:
Mark Moser 2016-08-16 12:08:13 -05:00
parent 114450dd08
commit 10d69f3664
11 changed files with 203 additions and 175 deletions

View File

@ -43,11 +43,6 @@ Metrics/AbcSize:
- db/migrate/**/* - db/migrate/**/*
Max: 20 Max: 20
# TODO: remove this cop exception after refactor
Metrics/ClassLength:
Exclude:
- test/**/*
Metrics/LineLength: Metrics/LineLength:
Max: 110 Max: 110
Exclude: Exclude:

View File

@ -15,11 +15,16 @@
# #
# and, you'll have to watch "config/Guardfile" instead of "Guardfile" # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
guard :minitest, spring: true do # , all_after_pass: true guard :minitest, spring: "bin/rails test", all_after_pass: true, all_on_start: false do
watch(%r{^test/test_helper\.rb$}) { 'test' } watch(%r{^test/test_helper\.rb$}) { 'test' }
watch(%r{^test/(.*)\/?(.*)_test\.rb$}) watch(%r{^test/(.*)\/?(.*)_test\.rb$})
watch(%r{^app/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/lib/#{m[1]}#{m[2]}_test.rb" } # run tests when touching files matching: /app/**/*.rb
watch(%r{^app/(.*/)?([^/]+)\.rb$}) { |m| ["test/#{m[1]}#{m[2]}_test.rb", "test/#{m[1]}#{m[2]}"] }
# run tests when touching files matching: /lib/**/*.rb
watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| ["test/lib/#{m[1]}#{m[2]}_test.rb", "test/lib/#{m[1]}#{m[2]}"] }
end end
guard 'livereload' do guard 'livereload' do

View File

@ -8,7 +8,8 @@
"jquery": "1.9.1", "jquery": "1.9.1",
"jquery-validate": "", "jquery-validate": "",
"tota11y": "", "tota11y": "",
"modernizr-lite": "*" "modernizr-lite": "*",
"html5shiv": "^3.7.3"
}, },
"ignore": [ "ignore": [
"**/.*", "**/.*",
@ -18,6 +19,5 @@
"tests" "tests"
], ],
"devDependencies": { "devDependencies": {
"html5shiv": "^3.7.3"
} }
} }

View File

@ -4,6 +4,8 @@ require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help' require 'rails/test_help'
require "minitest/autorun" require "minitest/autorun"
require 'minitest/reporters' require 'minitest/reporters'
Dir[Rails.root.join("test/test_helpers/**/*.rb")].each { |f| require f }
Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)] Minitest::Reporters.use! [Minitest::Reporters::DefaultReporter.new(color: true)]
class ActiveSupport::TestCase class ActiveSupport::TestCase

View File

@ -0,0 +1,25 @@
Use this folder to store mocks, stubs, etc to make isolated testing possible.
Some definitions borrowed from:
http://martinfowler.com/articles/mocksArentStubs.html
* Dummy
objects are passed around but never actually used.
Usually they are just used to fill parameter lists.
* Fake
objects actually have working implementations, but usually
take some shortcut which makes them not suitable for
production (an in memory database is a good example).
* Stubs
provide canned answers to calls made during the test,
usually not responding at all to anything outside what's
programmed in for the test. Stubs may also record information
about calls, such as an email gateway stub that remembers the
messages it 'sent', or maybe only how many messages it 'sent'.
* Mocks
objects pre-programmed with expectations which form a
specification of the calls they are expected to receive.

View File

@ -0,0 +1,18 @@
class AnswerValidatable
include ActiveModel::Validations
attr_accessor :answer
attr_accessor :question
validates :answer, answer_format: true
MockQuestion = Struct.new(:input_type)
def initialize input_type
@input_type = input_type
end
def question
MockQuestion.new(@input_type)
end
end

View File

@ -0,0 +1,35 @@
require 'test_helper'
class AnswerFormatValidatorTest < ActiveSupport::TestCase
test "checkbox should PASS with populated array" do
obj = AnswerValidatable.new('checkbox')
obj.answer = ["", "", "valid answer"]
assert obj.valid?
assert obj.errors.messages.empty?
end
test "checkbox should FAIL with nil" do
obj = AnswerValidatable.new('checkbox')
obj.answer = nil
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "checkbox should FAIL with empty string" do
obj = AnswerValidatable.new('checkbox')
obj.answer = " "
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "checkbox should FAIL with array of empty strings" do
obj = AnswerValidatable.new('checkbox')
obj.answer = ["", "", " "]
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
end

View File

@ -0,0 +1,51 @@
require 'test_helper'
class AnswerFormatValidatorTest < ActiveSupport::TestCase
test "live_code should PASS with populated hash" do
obj = AnswerValidatable.new('live_code')
obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code should PASS with finish later" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "true" }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code should FAIL with nil" do
obj = AnswerValidatable.new('live_code')
obj.answer = nil
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code should FAIL without checking finish later" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "" }
refute obj.valid?
assert_match(/come back/, obj.errors.messages[:answer][0])
end
test "live_code should FAIL without values" do
obj = AnswerValidatable.new('live_code')
obj.answer = { text: "", html: "", css: "", js: "" }
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code should PASS with text only" do
obj = AnswerValidatable.new('live_code')
obj.answer = { html: "", css: "", js: "", text: "reasons" }
assert obj.valid?
assert obj.errors.messages.empty?
end
end

View File

@ -0,0 +1,27 @@
require 'test_helper'
class AnswerFormatValidatorTest < ActiveSupport::TestCase
test "radio should pass with string" do
obj = AnswerValidatable.new('radio')
obj.answer = 'option-1'
assert obj.valid?
assert obj.errors.messages.empty?
end
test "radio should FAIL with nil" do
obj = AnswerValidatable.new('radio')
obj.answer = nil
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "radio should FAIL with empty string" do
obj = AnswerValidatable.new('radio')
obj.answer = ''
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
end

View File

@ -0,0 +1,35 @@
require 'test_helper'
class AnswerFormatValidatorTest < ActiveSupport::TestCase
test "text should PASS with string" do
obj = AnswerValidatable.new('text')
obj.answer = "this is a valid answer"
assert obj.valid?
assert obj.errors.messages.empty?
end
test "text should FAIL with nil" do
obj = AnswerValidatable.new('text')
obj.answer = nil
refute obj.valid?
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
end
test "text should FAIL with empry string" do
obj = AnswerValidatable.new('text')
obj.answer = " "
refute obj.valid?
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
end
test "text should FAIL with more than 1000 charactures" do
obj = AnswerValidatable.new('text')
obj.answer = SecureRandom.urlsafe_base64(1001)
refute obj.valid?
assert_match(/char.*limit.*1000.$/, obj.errors.messages[:answer][0])
end
end

View File

@ -1,165 +0,0 @@
require 'test_helper'
class AnswerValidatable
include ActiveModel::Validations
attr_accessor :answer
attr_accessor :question
validates :answer, answer_format: true
MockQuestion = Struct.new(:input_type)
def initialize input_type
@input_type = input_type
end
def question
MockQuestion.new(@input_type)
end
end
class AnswerFormatValidatorTest < ActiveSupport::TestCase
# input_type RADIO
test "radio should pass with string" do
obj = AnswerValidatable.new('radio')
obj.answer = 'option-1'
assert obj.valid?
assert obj.errors.messages.empty?
end
test "radio should FAIL with nil" do
obj = AnswerValidatable.new('radio')
obj.answer = nil
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "radio should FAIL with empty string" do
obj = AnswerValidatable.new('radio')
obj.answer = ''
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
# input_type TEXT
test "text should PASS with string" do
obj = AnswerValidatable.new('text')
obj.answer = "this is a valid answer"
assert obj.valid?
assert obj.errors.messages.empty?
end
test "text should FAIL with nil" do
obj = AnswerValidatable.new('text')
obj.answer = nil
refute obj.valid?
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
end
test "text should FAIL with empry string" do
obj = AnswerValidatable.new('text')
obj.answer = " "
refute obj.valid?
assert_match(/enter.*answer/, obj.errors.messages[:answer][0])
end
test "text should FAIL with more than 1000 charactures" do
obj = AnswerValidatable.new('text')
obj.answer = SecureRandom.urlsafe_base64(1001)
refute obj.valid?
assert_match(/char.*limit.*1000.$/, obj.errors.messages[:answer][0])
end
# input_type CHECK BOX
test "checkbox should PASS with populated array" do
obj = AnswerValidatable.new('checkbox')
obj.answer = ["", "", "valid answer"]
assert obj.valid?
assert obj.errors.messages.empty?
end
test "checkbox should FAIL with nil" do
obj = AnswerValidatable.new('checkbox')
obj.answer = nil
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "checkbox should FAIL with empty string" do
obj = AnswerValidatable.new('checkbox')
obj.answer = " "
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
test "checkbox should FAIL with array of empty strings" do
obj = AnswerValidatable.new('checkbox')
obj.answer = ["", "", " "]
refute obj.valid?
assert_match(/select.*answer/, obj.errors.messages[:answer][0])
end
# input_type LIVE CODER
test "live_code should PASS with populated hash" do
obj = AnswerValidatable.new('live_code')
obj.answer = { html: 'this is html', css: '', js: '', text: 'some reasons' }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code should PASS with finish later" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "true" }
assert obj.valid?
assert obj.errors.messages.empty?
end
test "live_code should FAIL with nil" do
obj = AnswerValidatable.new('live_code')
obj.answer = nil
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code should FAIL without checking finish later" do
obj = AnswerValidatable.new('live_code')
obj.answer = { later: "" }
refute obj.valid?
assert_match(/come back/, obj.errors.messages[:answer][0])
end
test "live_code should FAIL without values" do
obj = AnswerValidatable.new('live_code')
obj.answer = { text: "", html: "", css: "", js: "" }
refute obj.valid?
assert_match(/write.*code/, obj.errors.messages[:answer][0])
end
test "live_code should PASS with text only" do
obj = AnswerValidatable.new('live_code')
obj.answer = { html: "", css: "", js: "", text: "reasons" }
assert obj.valid?
assert obj.errors.messages.empty?
end
end