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

Add spec expectation have_delivered_emails #45

Merged
merged 3 commits into from
Dec 29, 2020
Merged
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
33 changes: 25 additions & 8 deletions spec/expectations_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@ require "./spec_helper"
include Carbon::Expectations

describe Carbon::Expectations do
it "can check for delivered emails" do
adapter = Carbon::DevAdapter.new
email = FakeEmail.new(subject: "Sent email")
other_email = FakeEmail.new(subject: "Other email")
email.should_not be_delivered
describe "#be_delivered" do
it "can check for delivered emails" do
adapter = Carbon::DevAdapter.new
email = FakeEmail.new(subject: "Sent email")
other_email = FakeEmail.new(subject: "Other email")
email.should_not be_delivered

adapter.deliver_now(email)
adapter.deliver_now(email)

email.should be_delivered
other_email.should_not be_delivered
email.should be_delivered
other_email.should_not be_delivered
end
end

describe "#have_delivered_emails" do
it "can check that emails were not sent" do
Carbon.should_not have_delivered_emails
end

it "can check that emails were sent" do
adapter = Carbon::DevAdapter.new
email = FakeEmail.new(subject: "Sent email")

adapter.deliver_now(email)

Carbon.should have_delivered_emails
end
end
end
39 changes: 6 additions & 33 deletions src/carbon/expectations.cr
Original file line number Diff line number Diff line change
@@ -1,38 +1,11 @@
module Carbon::Expectations
struct BeDeliveredExpectation
def match(email : Carbon::Email) : Bool
Carbon::DevAdapter.delivered?(email)
end

def failure_message(email)
FailureMessage.new(email).build
end

def negative_failure_message(email)
"Expected: #{email} not to be delivered"
end
end

class FailureMessage
private getter email

def initialize(@email : Carbon::Email)
end

def build
String.build do |message|
message << "Expected: #{email} to be delivered"
if Carbon::DevAdapter.delivered_emails.empty?
message << ", but no emails were delivered"
else
message << "\n\nTry this..."
message << "\n\n ▸ See what emails were delivered with 'p Carbon::DevAdapter.delivered_emails'"
end
end
end
end
require "./expectations/*"

module Carbon::Expectations
private def be_delivered
BeDeliveredExpectation.new
end

private def have_delivered_emails
HaveDeliveredEmailsExpectation.new
end
end
21 changes: 21 additions & 0 deletions src/carbon/expectations/be_delivered_expectation.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
struct Carbon::Expectations::BeDeliveredExpectation
def match(email : Carbon::Email) : Bool
Carbon::DevAdapter.delivered?(email)
end

def failure_message(email)
String.build do |message|
message << "Expected: #{email} to be delivered"
if Carbon::DevAdapter.delivered_emails.empty?
message << ", but no emails were delivered"
else
message << "\n\nTry this..."
message << "\n\n ▸ See what emails were delivered with 'p Carbon::DevAdapter.delivered_emails'"
end
end
end

def negative_failure_message(email)
"Expected: #{email} not to be delivered"
end
end
13 changes: 13 additions & 0 deletions src/carbon/expectations/have_delivered_emails_expectation.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Carbon::Expectations::HaveDeliveredEmailsExpectation
def match(_carbon : Carbon.class) : Bool
Carbon::DevAdapter.delivered_emails.any?
end

def failure_message(_carbon : Carbon.class)
"Expected: Carbon to have delivered emails, but found none"
end

def negative_failure_message(_carbon : Carbon.class)
"Expected: Carbon to have no delivered emails, but found some"
end
end