Generate a DiceRoll when rolling

This commit is contained in:
Trevor Vallender 2024-06-13 13:03:34 +01:00
parent 0658eca783
commit 5f682dff6e
3 changed files with 16 additions and 3 deletions

View File

@ -16,8 +16,10 @@ class Stat < ApplicationRecord
numericality: true
validate :validate_roll_command
def roll
DiceRoller.new(roll_command, stat: self).roll
def roll(table)
result = DiceRoller.new(roll_command, stat: self).roll
dice_rolls.create(result:, table:)
result
end
private

View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
require "test_helper"
class DiceRollingTest < ActionDispatch::IntegrationTest
test "rolling a stat should create a dice roll" do
assert_changes("DiceRoll.count", +1) do
stats(:strength).roll(tables(:dnd_table))
end
end
end

View File

@ -26,6 +26,6 @@ class StatTest < ActiveSupport::TestCase
test "rolls with roll_command" do
stat = stats(:strength)
stat.roll_command = "1d6"
100.times { assert (1..6).cover?(stat.roll) }
100.times { assert (1..6).cover?(stat.roll(tables(:dnd_table))) }
end
end