Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ end
MyExperiment.raise_on_mismatches = true
```

Subclasses of `MyExperiment` inherit this setting unless they set their own.

Scientist will raise a `Scientist::Experiment::MismatchError` exception if any observations don't match.

#### Custom mismatch errors
Expand Down
9 changes: 8 additions & 1 deletion lib/scientist/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,15 @@ def raise_on_mismatches=(bool)
end

# Whether or not to raise a mismatch error when a mismatch occurs.
#
# Subclasses inherit the setting from their superclass unless they set
# their own.
def raise_on_mismatches?
@raise_on_mismatches
if !@raise_on_mismatches.nil?
@raise_on_mismatches
elsif respond_to?(:superclass) && superclass.respond_to?(:raise_on_mismatches?)
superclass.raise_on_mismatches?
end
end
end

Expand Down
23 changes: 23 additions & 0 deletions test/scientist/experiment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,29 @@ def @ex.raised(op, exception)

assert_raises(Scientist::Experiment::MismatchError) { @ex.run }
end

it "inherits the raise_on_mismatches class attribute from a superclass" do
subclass = Class.new(Fake)
Fake.raise_on_mismatches = true

ex = subclass.new
ex.use { "fine" }
ex.try { "not fine" }

assert_raises(Scientist::Experiment::MismatchError) { ex.run }
end

it "lets a subclass override the raise_on_mismatches class attribute" do
subclass = Class.new(Fake)
Fake.raise_on_mismatches = true
subclass.raise_on_mismatches = false

ex = subclass.new
ex.use { "fine" }
ex.try { "not fine" }

assert_equal "fine", ex.run
end
end

describe "MismatchError" do
Expand Down