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

doc: add shared examples for line checking #28

Merged
merged 1 commit into from Mar 24, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions README.md
Expand Up @@ -196,6 +196,57 @@ specify "when event is not found" do
end
```

To test line used (email, push, pigeon), you can the following shared examples:

```ruby
##
# Line checker for `active_delivery`
#
# ==== Attributes
#
# * +lines+ - Array of possible lines (:mailer, :pusher, :pigeon..)
# * +methods+ - methods called on each line
shared_examples_for "deliverable" do |lines, methods|
it "calls correct lines and methods" do
lines.each do |line|
methods.each do |method|
expect(described_class.delivery_lines[line]).to receive(:notify).with(method, anything).once
subject
end
end
end
end

##
# Line non-checker for `active_delivery`
#
# ==== Attributes
#
# * +lines+ - Array of possible lines (:mailer, :pusher)
# * +methods+ - methods called on each line
shared_examples_for "not_deliverable" do |lines, methods|
it "calls correct lines and methods" do
lines.each do |line|
methods.each do |method|
expect(described_class.delivery_lines[line]).not_to receive(:notify).with(method, anything)
subject
end
end
end
end
```

and call them like that:

```ruby
RSpec.describe V1::MyDelivery do
context "when..." do
it_behaves_like "deliverable", [:mailer], [:some_action]
Copy link
Owner

Choose a reason for hiding this comment

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

I like the idea.

Can we make the API a bit more explicit? For example:

# single line
it_behaves_like "deliverable", via: :mailer, action: :some_action
# multiple lines
it_behaves_like "deliverable", via: [:mailer, :pusher], action: :some_action

Btw, what is the case for multiple actions? I think, the most common case is to check for a single action; hence, for multiple actions we can just include the shared example multiple times.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I forgot this one! I'll take a look asap. 🙂

For the multiple actions, it was only to avoid to copy/paste the same shared example.

Copy link
Owner

Choose a reason for hiding this comment

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

No worries. I think, we can actually merge this.

it_behaves_like "not_deliverable", [:pusher], [:some_action]
end
end
```

**NOTE:** test mode activated automatically if `RAILS_ENV` or `RACK_ENV` env variable is equal to "test". Otherwise add `require "active_delivery/testing/rspec"` to your `spec_helper.rb` / `rails_helper.rb` manually. This is also required if you're using Spring in test environment (e.g. with help of [spring-commands-rspec](https://github.com/jonleighton/spring-commands-rspec)).

## Custom "lines"
Expand Down