Skip to content

Commit

Permalink
Add arguments to exceptions risen by stubbed methods
Browse files Browse the repository at this point in the history
Use the following syntax:

```yaml
---
  - class: API
    chain: find_product
    arguments:
      - 1
    actions:
      - raise: API::NotFound
        arguments: # <-- The new option
	  - "Product with id: 1 not found"
```

In this case the following exception will be risen:

```ruby
raise API::NotFoundError.new("Product with id: 1 not found")
```

You can add more arguments to custom errors as well.
  • Loading branch information
nepalez committed Mar 8, 2020
1 parent 22c0a6d commit cd84d8b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ For http requests:
- return: true
repeate: 1 # this is the default value
- raise: ActiveRecord::RecordNotFound
arguments:
- "Profile with id: 1 not found" # for error message
- const: NOTIFIER_TIMEOUT_SEC
value: 10
Expand Down
18 changes: 13 additions & 5 deletions lib/fixturama/changes/chain/raise_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,24 @@ def call
def initialize(**options)
@error = error_from options
@repeat = repeat_from options
rescue StandardError => err
raise Fixturama::FixtureError.new("an exception class", options, err)
end

def error_from(options)
case value = options[:raise]
when NilClass, TrueClass, "true" then StandardError
when Class then value < Exception ? value : raise("Not an exception")
klass = klass_from(options)
params = options[:arguments]
params.is_a?(Array) ? klass.new(*params) : klass
end

def klass_from(options)
klass = case value = options[:raise]
when NilClass, TrueClass, "true" then StandardError
when Class then value
else Kernel.const_get(value)
end
rescue StandardError => err
raise Fixturama::FixtureError.new("an exception class", options, err)

klass < Exception ? klass : raise("#{klass} is not an exception")
end

def repeat_from(options)
Expand Down
7 changes: 5 additions & 2 deletions spec/fixturama/stub_fixture/_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ def pay(_)
let(:arguments) { [0] }

it "raises an exception" do
expect { subject }.to raise_error ArgumentError
expect { subject }.to raise_error do |ex|
expect(ex).to be_kind_of(ArgumentError)
expect(ex.message).to eq "Something got wrong"
end
end
end

Expand Down Expand Up @@ -59,7 +62,7 @@ def pay(_)
end

context "with partially defined options" do
subject { Payment.new.pay(10, overdraft: true, notiy: true) }
subject { Payment.new.pay(10, overdraft: true, notify: true) }

it "uses the stub" do
expect(subject).to eq(-5)
Expand Down
2 changes: 2 additions & 0 deletions spec/fixturama/stub_fixture/stub.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- 0
actions:
- raise: ArgumentError
arguments:
- Something got wrong

- class: Payment
chain:
Expand Down

0 comments on commit cd84d8b

Please sign in to comment.