Skip to content

Instantly share code, notes, and snippets.

@drench
Created January 2, 2025 17:15
Show Gist options
  • Save drench/bd1220c97ce1016973b9ea53a9d2fc3f to your computer and use it in GitHub Desktop.
Save drench/bd1220c97ce1016973b9ea53a9d2fc3f to your computer and use it in GitHub Desktop.
Trying some things out with Data.define
RSpec.describe Data do
subject { data.new(**input) }
let(:data) { Data.define(:a, :b) }
context "when providing all arguments" do
context "with string keys" do
let(:input) { { "a" => 1, "b" => true } }
it { is_expected.to have_attributes(a: 1, b: true) }
end
context "with symbol keys" do
let(:input) { { a: 1, b: true } }
it { is_expected.to have_attributes(a: 1, b: true) }
end
context "with mixed keys" do
let(:input) { { "a" => 1, :b => true } }
it { is_expected.to have_attributes(a: 1, b: true) }
end
context "with nil values" do
let(:input) { { a: nil, b: nil } }
it { is_expected.to have_attributes(a: nil, b: nil) }
end
end
context "when providing extra arguments" do
let(:input) { { a: 1, b: true, c: :sea } }
it { expect { subject }.to raise_error(ArgumentError) }
end
context "when providing too few arguments" do
let(:input) { { a: 1 } }
it { expect { subject }.to raise_error(ArgumentError) }
end
end
@drench
Copy link
Author

drench commented Jan 2, 2025

rspec output:

Data
  when providing all arguments
    with string keys
      is expected to have attributes {:a => 1, :b => true}
    with symbol keys
      is expected to have attributes {:a => 1, :b => true}
    with mixed keys
      is expected to have attributes {:a => 1, :b => true}
    with nil values
      is expected to have attributes {:a => nil, :b => nil}
  when providing extra arguments
    is expected to raise ArgumentError
  when providing too few arguments
    is expected to raise ArgumentError

Finished in 0.00224 seconds (files took 0.04984 seconds to load)
6 examples, 0 failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment