Skip to content

Commit

Permalink
Use one-liners expectations in specs
Browse files Browse the repository at this point in the history
Where suitable, expectations are rewritten to make use or RSpec one-liners
  • Loading branch information
oldjackson committed Mar 25, 2020
1 parent 5c2c0e4 commit 3e733a3
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions core/spec/models/spree/concerns/user_address_book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ module Spree
context "saving a shipping address" do
let(:user_address) { user.user_addresses.find_first_by_address_values(address.attributes) }

subject { user.save_in_address_book(address.attributes, true) }
subject do
-> { user.save_in_address_book(address.attributes, true) }
end

context "the address is a new record" do
let(:address) { build(:address) }

it "creates a new Address" do
expect { subject }.to change { Spree::Address.count }.by(1)
is_expected.to change { Spree::Address.count }.by(1)
end

it "creates a UserAddress" do
expect { subject }.to change { Spree::UserAddress.count }.by(1)
is_expected.to change { Spree::UserAddress.count }.by(1)
end

it "sets the UserAddress default flag to true" do
subject
subject.call
expect(user_address.default).to eq true
expect(user_address.default_billing).to be_falsey
end
Expand All @@ -44,7 +46,7 @@ module Spree
end

it "adds the address to the user's the associated addresses" do
expect { subject }.to change { user.reload.addresses.count }.by(1)
is_expected.to change { user.reload.addresses.count }.by(1)
end
end

Expand All @@ -62,12 +64,8 @@ module Spree

context "saving a shipping address" do
context "makes all the other associated shipping addresses not be the default and ignores the billing ones" do
it do
expect { subject }.to change { original_user_address.reload.default }.from(true).to(false)
end
it do
expect { subject }.not_to change { original_user_bill_address.reload.default_billing }
end
it { is_expected.to change { original_user_address.reload.default }.from(true).to(false) }
it { is_expected.not_to change { original_user_bill_address.reload.default_billing } }
end

context "an odd flip-flop corner case discovered running backfill rake task" do
Expand All @@ -87,15 +85,11 @@ module Spree
end

context "saving a billing address" do
subject { user.save_in_address_book(address.attributes, true, :billing) }
subject { -> { user.save_in_address_book(address.attributes, true, :billing) } }

context "makes all the other associated billing addresses not be the default and ignores the shipping ones" do
it do
expect { subject }.not_to change { original_user_address.reload.default }
end
it do
expect { subject }.to change { original_user_bill_address.reload.default_billing }.from(true).to(false)
end
it { is_expected.not_to change { original_user_address.reload.default } }
it { is_expected.to change { original_user_bill_address.reload.default_billing }.from(true).to(false) }
end

context "an odd flip-flop corner case discovered running backfill rake task" do
Expand Down Expand Up @@ -123,12 +117,12 @@ module Spree

context "properly sets the default flag" do
context "shipping address" do
it { expect(subject).to eq user.ship_address }
it { expect(subject.call).to eq user.ship_address }
end

context "billing address" do
subject { user.save_in_address_book(address.attributes, true, :billing) }
it { expect(subject).to eq user.bill_address }
it { is_expected.to eq user.bill_address }
end
end

Expand All @@ -151,12 +145,12 @@ module Spree

context "is the new default" do
context "shipping address" do
it { expect(subject).to eq user.ship_address }
it { is_expected.to eq user.ship_address }
end

context "billing address" do
subject { user.save_in_address_book(address.attributes, true, :billing) }
it { expect(subject).to eq user.bill_address }
it { is_expected.to eq user.bill_address }
end
end
end
Expand Down Expand Up @@ -496,7 +490,7 @@ module Spree
let!(:user) { create(:user) }
let!(:address) { create(:address) }

# https://github.com/solidusio/solidus/issuesÎ/1241
# https://github.com/solidusio/solidus/issues/1241
it "resets the association and persists" do
# Load (which will cache) the has_one association
expect(user.bill_address).to be_nil
Expand Down

0 comments on commit 3e733a3

Please sign in to comment.