Generate a DiceRoll when rolling
This commit is contained in:
parent
0658eca783
commit
5f682dff6e
|
@ -16,8 +16,10 @@ class Stat < ApplicationRecord
|
||||||
numericality: true
|
numericality: true
|
||||||
validate :validate_roll_command
|
validate :validate_roll_command
|
||||||
|
|
||||||
def roll
|
def roll(table)
|
||||||
DiceRoller.new(roll_command, stat: self).roll
|
result = DiceRoller.new(roll_command, stat: self).roll
|
||||||
|
dice_rolls.create(result:, table:)
|
||||||
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -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
|
|
@ -26,6 +26,6 @@ class StatTest < ActiveSupport::TestCase
|
||||||
test "rolls with roll_command" do
|
test "rolls with roll_command" do
|
||||||
stat = stats(:strength)
|
stat = stats(:strength)
|
||||||
stat.roll_command = "1d6"
|
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
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue