From 5f682dff6e017da7e3758d67520c7235d7d1ef47 Mon Sep 17 00:00:00 2001 From: Trevor Vallender Date: Thu, 13 Jun 2024 13:03:34 +0100 Subject: [PATCH] Generate a DiceRoll when rolling --- app/models/stat.rb | 6 ++++-- test/integration/dice_rolling_test.rb | 11 +++++++++++ test/models/stat_test.rb | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 test/integration/dice_rolling_test.rb diff --git a/app/models/stat.rb b/app/models/stat.rb index 5089aab..0ac5796 100644 --- a/app/models/stat.rb +++ b/app/models/stat.rb @@ -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 diff --git a/test/integration/dice_rolling_test.rb b/test/integration/dice_rolling_test.rb new file mode 100644 index 0000000..c71515a --- /dev/null +++ b/test/integration/dice_rolling_test.rb @@ -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 diff --git a/test/models/stat_test.rb b/test/models/stat_test.rb index 760c6a9..283152a 100644 --- a/test/models/stat_test.rb +++ b/test/models/stat_test.rb @@ -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