diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index cddf51f..4346360 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,15 +24,16 @@ jobs: - ruby: "3.3" ### TEST RAILS VERSIONS - ruby: "2.7" - rails_version: "~> 6.0" + rails_version: "~> 6.0.0" - ruby: "2.7" - rails_version: "~> 6.1" + rails_version: "~> 6.1.0" - ruby: "3.3" - rails_version: "~> 7.0" + rails_version: "~> 7.0.0" + db_gem_version: "~> 1.4" # fixes sqlite3 gem dependency issue - ruby: "3.3" - rails_version: "~> 7.1" + rails_version: "~> 7.1.0" - ruby: "3.3" - rails_version: "~> 7.2" + rails_version: "~> 7.2.0" steps: - uses: actions/checkout@v3 diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 63d3833..e46580f 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -22,3 +22,43 @@ config.filter_rails_from_backtrace! end + +if Rails::VERSION::STRING.to_f <= 6.0 + def assert_no_difference(expression, message = nil, &block) + assert_difference expression, 0, message, &block + end + + def assert_difference(expression, *args, &block) + expressions = + if expression.is_a?(Hash) + message = args[0] + expression + else + difference = args[0] || 1 + message = args[1] + Array(expression).index_with(difference) + end + + exps = expressions.keys.map { |e| + e.respond_to?(:call) ? e : lambda { eval(e, block.binding) } + } + before = exps.map(&:call) + + retval = assert_nothing_raised(&block) + + expressions.zip(exps, before) do |(code, diff), exp, before_value| + actual = exp.call + error = "#{code.inspect} didn't change by #{diff}, but by #{actual - before_value}" + error = "#{message}.\n#{error}" if message + assert_equal(before_value + diff, actual, error) + end + + retval + end + + def assert_nothing_raised + yield.tap { assert(true) } + rescue => error + raise Minitest::UnexpectedError.new(error) + end +end