Skip to content

Commit

Permalink
Namespace tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olejrosendahl committed Sep 21, 2015
1 parent 339ce18 commit 86891b2
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 147 deletions.
36 changes: 19 additions & 17 deletions spec/validation_error_reporter/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
require "spec_helper"

describe ValidationErrorReporter::Configuration do
module ValidationErrorReporter
describe Configuration do

it { is_expected.to respond_to(:to) }
it { is_expected.to respond_to(:from) }
it { is_expected.to respond_to(:profiler) }
it { is_expected.to respond_to(:notifiers) }
it { is_expected.to respond_to(:to) }
it { is_expected.to respond_to(:from) }
it { is_expected.to respond_to(:profiler) }
it { is_expected.to respond_to(:notifiers) }

describe "initialize(options)" do
it "profiles by default" do
expect(subject.profiler).to be_instance_of(ValidationErrorReporter::Profiler)
end
describe "initialize(options)" do
it "profiles by default" do
expect(subject.profiler).to be_instance_of(Profiler)
end

context "when not options present" do
it "adds a console notifer to notifiers" do
expect(subject.notifiers.first).to be_an_instance_of ValidationErrorReporter::Notifiers::Console
context "when not options present" do
it "adds a console notifer to notifiers" do
expect(subject.notifiers.first).to be_an_instance_of Notifiers::Console
end
end
end

context "when 'to' and 'from' is present" do
let(:subject) { described_class.new(from: "from", to: "to") }
context "when 'to' and 'from' is present" do
let(:subject) { described_class.new(from: "from", to: "to") }

it "adds a email notifier to notifiers" do
expect(subject.notifiers.last).to be_an_instance_of ValidationErrorReporter::Notifiers::Email
it "adds a email notifier to notifiers" do
expect(subject.notifiers.last).to be_an_instance_of Notifiers::Email
end
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions spec/validation_error_reporter/entity_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
require "spec_helper"

describe ValidationErrorReporter::Entity do

describe ".models_for(model_names)" do
it "returns all models" do
expect(described_class.models_for).to eq([Company, Project])
end
module ValidationErrorReporter
describe Entity do
describe ".models_for(model_names)" do
it "returns all models" do
expect(described_class.models_for).to eq([Company, Project])
end

context "when when given a list of models as strings" do
it "resolved the models that exists in the application" do
expect(described_class.models_for(["Company", "Model1"])).to eq([Company])
context "when when given a list of models as strings" do
it "resolved the models that exists in the application" do
expect(described_class.models_for(["Company", "Model1"])).to eq([Company])
end
end
end
end

end
65 changes: 34 additions & 31 deletions spec/validation_error_reporter/error_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
require "spec_helper"

describe ValidationErrorReporter::Error do
subject do
described_class.new(double(
id: 1,
class: double(
model_name: double(name: "Project", human: "Project"),
count: 1,
primary_key: :id
),
errors: double(
full_messages: [
"Name can't be blank",
"Name is too short (minimum is 2 characters)"]
)
))
end
module ValidationErrorReporter
describe Error do
subject do
described_class.new(double(
id: 1,
class: double(
model_name: double(name: "Project", human: "Project"),
count: 1,
primary_key: :id
),
errors: double(
full_messages: [
"Name can't be blank",
"Name is too short (minimum is 2 characters)"]
)
))
end

describe "#to_s" do
it "outputs the records with the errors" do
expect(subject.to_s).to eq(
"Project 1:\n" +
" Name can't be blank\n" +
" Name is too short (minimum is 2 characters)"
)
describe "#to_s" do
it "outputs the records with the errors" do
expect(subject.to_s).to eq(
"Project 1:\n" +
" Name can't be blank\n" +
" Name is too short (minimum is 2 characters)"
)
end
end
end

describe "#model_name" do
it "returns the name of the wrapped model record" do
expect(subject.model_name).to eq("Project")
describe "#model_name" do
it "returns the name of the wrapped model record" do
expect(subject.model_name).to eq("Project")
end
end
end

describe "#model_total_count" do
it "return the total number of records for the model" do
expect(subject.model_total_count).to eq(1)
describe "#model_total_count" do
it "return the total number of records for the model" do
expect(subject.model_total_count).to eq(1)
end
end
end

end
12 changes: 7 additions & 5 deletions spec/validation_error_reporter/formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require "spec_helper"

describe ValidationErrorReporter::Formatter do
module ValidationErrorReporter
describe Formatter do

describe "#format(message)" do
it "outpus a formatted message" do
expect(subject.format(:message)).to eq(:message)
describe "#format(message)" do
it "outpus a formatted message" do
expect(subject.format(:message)).to eq(:message)
end
end
end

end
end
19 changes: 10 additions & 9 deletions spec/validation_error_reporter/notifiers/console_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require "spec_helper"

describe ValidationErrorReporter::Notifiers::Console do
let(:subject) { described_class.new(double) }
module ValidationErrorReporter
describe Notifiers::Console do
let(:subject) { described_class.new(double) }

describe "#notify" do
it "sends its message to it's output" do
printer = double
notifier = described_class.new(double, printer)
describe "#notify" do
it "sends its message to it's output" do
printer = double
notifier = described_class.new(double, printer)

expect(printer).to receive(:puts).with(:message)
expect(printer).to receive(:puts).with(:message)

notifier.notify(:message)
notifier.notify(:message)
end
end
end

end
44 changes: 23 additions & 21 deletions spec/validation_error_reporter/notifiers/email_spec.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
require "spec_helper"

describe ValidationErrorReporter::Notifiers::Email do
include Mail::Matchers
module ValidationErrorReporter
describe Notifiers::Email do
include Mail::Matchers

let(:configuration) do
ValidationErrorReporter::Configuration.new(from: "from@example.com", to: "to@example.com")
end
let(:subject) { described_class.new(configuration) }

describe "#notify" do
it "accumulates the notifications" do
error = double
subject.notify(error)
expect(subject.instance_exec { @output }).to eq([error])
let(:configuration) do
Configuration.new(from: "from@example.com", to: "to@example.com")
end
end
let(:subject) { described_class.new(configuration) }

describe "#finalize" do
before do
subject.finalize
describe "#notify" do
it "accumulates the notifications" do
error = double
subject.notify(error)
expect(subject.instance_exec { @output }).to eq([error])
end
end

it { is_expected.to have_sent_email }
it { is_expected.to have_sent_email.from("from@example.com") }
it { is_expected.to have_sent_email.to("to@example.com") }
it { is_expected.to have_sent_email.with_subject("ValidationErrorReporter") }
it { is_expected.to have_sent_email.with_body("") }
describe "#finalize" do
before do
subject.finalize
end

it { is_expected.to have_sent_email }
it { is_expected.to have_sent_email.from("from@example.com") }
it { is_expected.to have_sent_email.to("to@example.com") }
it { is_expected.to have_sent_email.with_subject("ValidationErrorReporter") }
it { is_expected.to have_sent_email.with_body("") }
end
end
end
32 changes: 17 additions & 15 deletions spec/validation_error_reporter/profile_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
require "spec_helper"

describe ValidationErrorReporter::ProfileFormatter do
let(:profile) {{
"Project" => { count: 1, total: 1, rate: 100.0 }
}}
module ValidationErrorReporter
describe ProfileFormatter do
let(:profile) {{
"Project" => { count: 1, total: 1, rate: 100.0 }
}}

describe "#format(profile)" do
it "creates a summary string from the given hash" do
expect(subject.format(profile)).to eq(
"Summary:\n" +
"Model with most errors: Project (1 entries, 1 errors, 100.0%).\n" +
"Model with highest error rate: Project (1 entries, 1 errors, 100.0%)."
)
end
describe "#format(profile)" do
it "creates a summary string from the given hash" do
expect(subject.format(profile)).to eq(
"Summary:\n" +
"Model with most errors: Project (1 entries, 1 errors, 100.0%).\n" +
"Model with highest error rate: Project (1 entries, 1 errors, 100.0%)."
)
end

describe "when profile is empty" do
it "returns 'No errors'" do
expect(subject.format({})).to eq("No errors")
describe "when profile is empty" do
it "returns 'No errors'" do
expect(subject.format({})).to eq("No errors")
end
end
end
end
Expand Down
28 changes: 14 additions & 14 deletions spec/validation_error_reporter/profiler_spec.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
require "spec_helper"

describe ValidationErrorReporter::Profiler do
module ValidationErrorReporter
describe Profiler do
describe "#profile" do
it "returns a hash with stats" do
error_1 = double(model_name: "Project", model_total_count: 4)
error_2 = double(model_name: "Project", model_total_count: 4)

describe "#profile" do
it "returns a hash with stats" do
error_1 = double(model_name: "Project", model_total_count: 4)
error_2 = double(model_name: "Project", model_total_count: 4)

expect(subject.profile([error_1, error_2])).to eq({
"Project" => {
count: 2,
total: 4,
rate: 50.0,
},
})
expect(subject.profile([error_1, error_2])).to eq({
"Project" => {
count: 2,
total: 4,
rate: 50.0,
},
})
end
end
end

end
51 changes: 26 additions & 25 deletions spec/validation_error_reporter/reporter_spec.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
require "spec_helper"

describe ValidationErrorReporter::Reporter do
module ValidationErrorReporter
describe Reporter do

let(:configuration) { ValidationErrorReporter::Configuration.new }
let(:subject) { described_class.new(configuration) }
let(:configuration) { Configuration.new }
let(:subject) { described_class.new(configuration) }

describe "initialize(configration)" do
it "assigns notifiers from the configuration" do
expect(subject.notifiers).to eq(configuration.notifiers)
expect(subject.profiler).to eq(configuration.profiler)
describe "initialize(configration)" do
it "assigns notifiers from the configuration" do
expect(subject.notifiers).to eq(configuration.notifiers)
expect(subject.profiler).to eq(configuration.profiler)
end
end
end

describe "#report(error)" do
it "sends all the failed records to the notifiers" do
error = double(:to_s)
describe "#report(error)" do
it "sends all the failed records to the notifiers" do
error = double(:to_s)

expect(error).to receive(:to_s).exactly(1).times
expect_any_instance_of(ValidationErrorReporter::Notifiers::Console).to receive(:notify).exactly(1).times
expect(error).to receive(:to_s).exactly(1).times
expect_any_instance_of(Notifiers::Console).to receive(:notify).exactly(1).times

subject.report(error)
subject.report(error)
end
end
end

describe "#finalize" do
it "finalizes all notifiers" do
notifier = double
configuration = ValidationErrorReporter::Configuration.new
configuration.notifiers = [notifier]
describe "#finalize" do
it "finalizes all notifiers" do
notifier = double
configuration = Configuration.new
configuration.notifiers = [notifier]

reporter = described_class.new(configuration)
reporter = described_class.new(configuration)

expect(notifier).to receive(:notify).exactly(1).times
expect(notifier).to receive(:finalize).exactly(1).times
reporter.finalize
expect(notifier).to receive(:notify).exactly(1).times
expect(notifier).to receive(:finalize).exactly(1).times
reporter.finalize
end
end
end

end

0 comments on commit 86891b2

Please sign in to comment.