Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error handling: raise Chanko::ActiveIf::NoConditionFound for undefined conditions #35

Merged
merged 2 commits into from
Dec 6, 2013
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 8 additions & 1 deletion lib/chanko/active_if.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Chanko
class ActiveIf
class NoConditionFound < StandardError
end

class << self
def define(label, &block)
definitions[label] = block
Expand Down Expand Up @@ -57,7 +60,11 @@ def call(context, options)

def block
condition = @conditions.first
condition.is_a?(Block) ? condition : ActiveIf.find(condition)
if condition.kind_of?(Block)
condition
else
ActiveIf.find(condition) or raise NoConditionFound, condition
end
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/chanko/unit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ module Chanko
should be_false
end
end

context "when an undefined condition is specified" do
before do
unit.active_if(:this_is_not_a_condition)
end

specify "an undefined condition must raise NoConditionFound" do
begin
should be_nil
rescue Chanko::ActiveIf::NoConditionFound => e
e.message.should =~ /this_is_not_a_condition/
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use expect { ... }.to raise_error helper?
https://www.relishapp.com/rspec/rspec-expectations/v/3-0/docs/built-in-matchers/raise-error-matcher

expect { subject }.to raise_error(..., /.../)

end
end
end

describe ".any" do
Expand Down