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

Stop using RSpec is_expected with block expectations #4870

Merged
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
28 changes: 16 additions & 12 deletions core/spec/models/spree/address/state_validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
let(:state) { create :state, name: 'maryland', abbr: 'md', country: country }
let(:address) { build(:address, country: country) }

subject do
-> { described_class.new(address).perform }
end
subject { described_class.new(address).perform }

describe 'state attributes normalization' do
context "having a country with no states" do
Expand All @@ -21,7 +19,7 @@

it "nullifies the state attr" do
address.state = state
expect(subject).to change(address, :state).from(state).to(nil)
expect { subject }.to change(address, :state).from(state).to(nil)
end
end

Expand All @@ -39,8 +37,8 @@
end

it "nullifies the state_name if the state attr belongs to the country" do
expect(subject).to change(address, :state_name).from(state_name).to(nil)
expect(subject).to_not change(address, :state).from(state)
expect { subject }.to change(address, :state_name).from(state_name).to(nil)
expect { subject }.to_not change(address, :state).from(state)
end

context "belonging to a different country" do
Expand All @@ -49,7 +47,7 @@
end

it "doesn't nullify the state name" do
expect(subject).to_not change(address, :state_name).from(state_name)
expect { subject }.to_not change(address, :state_name).from(state_name)
end
end
end
Expand All @@ -62,7 +60,7 @@
end

it "sets the state having the specified state name" do
expect(subject).
expect { subject }.
to change{ [address.state, address.state_name] }.
from([nil, state.name]).
to([state, nil])
Expand All @@ -76,7 +74,7 @@
it "doesn't validate the state presence" do
address.state = nil
address.state_name = nil
subject.call
subject

expect(address.errors).to be_empty
end
Expand Down Expand Up @@ -107,14 +105,18 @@
it "state_name is not nil and country does not have any states" do
address.state = nil
address.state_name = 'alabama'
subject.call

subject

expect(address.errors).to be_empty
end

it "errors when state_name is nil" do
address.state_name = nil
address.state = nil
subject.call

subject

expect(address.errors.messages).to eq({ state: ["can't be blank"] })
end

Expand All @@ -127,7 +129,9 @@
it 'is invalid' do
address.country = italy
address.state = us_state
subject.call

subject

expect(address.errors["state"]).to eq(['does not match the country'])
end
end
Expand Down
6 changes: 3 additions & 3 deletions core/spec/models/spree/classification_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ module Spree
it "cannot link the same taxon to the same product more than once" do
product = create(:product)
taxon = create(:taxon)
add_taxon = lambda { product.taxons << taxon }
add_taxon.call
expect(add_taxon).to raise_error(ActiveRecord::RecordInvalid)
product.taxons << taxon

expect { product.taxons << taxon }.to raise_error(ActiveRecord::RecordInvalid)
end

let(:taxon_with_5_products) do
Expand Down
31 changes: 15 additions & 16 deletions core/spec/models/spree/concerns/user_address_book_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@ module Spree
context "saving a default shipping address" do
let(:user_address) { user.user_addresses.find_first_by_address_values(address.attributes) }
let(:force_default) { true }
subject do
-> { user.save_in_address_book(address.attributes, force_default) }
end
subject { user.save_in_address_book(address.attributes, force_default) }

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

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

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

it "sets the UserAddress default flag to true" do
subject.call
subject

expect(user_address.default).to eq true
expect(user_address.default_billing).to be_falsey
end
Expand All @@ -46,7 +45,7 @@ module Spree
end

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

Expand All @@ -65,8 +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 { is_expected.not_to change { original_user_address.reload.default }.from(true) }
it { is_expected.not_to change { original_user_bill_address.reload.default_billing } }
it { expect { subject }.not_to change { original_user_address.reload.default }.from(true) }
it { expect { subject }.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 @@ -85,11 +84,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 { 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) }
it { expect { subject }.not_to change { original_user_address.reload.default } }
it { expect { subject }.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 @@ -117,12 +116,12 @@ module Spree

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

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

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

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

context "billing address" do
subject { user.save_in_address_book(address.attributes, true, :billing) }
it { is_expected.to eq user.bill_address }
it { expect(subject).to eq user.bill_address }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/spec/models/spree/order/checkout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ def assert_state_changed(order, from, to)
end
specify do
transition = lambda { order.next! }
expect(transition).to raise_error(StateMachines::InvalidTransition, /#{I18n.t('spree.items_cannot_be_shipped')}/)
expect { transition.call }.to raise_error(StateMachines::InvalidTransition, /#{I18n.t('spree.items_cannot_be_shipped')}/)
end
end
end
Expand Down
8 changes: 4 additions & 4 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1541,26 +1541,26 @@ def generate
describe "#record_ip_address" do
let(:ip_address) { "127.0.0.1" }

subject { -> { order.record_ip_address(ip_address) } }
subject { order.record_ip_address(ip_address) }

it "updates the last used IP address" do
expect(subject).to change(order, :last_ip_address).to(ip_address)
expect { subject }.to change(order, :last_ip_address).to(ip_address)
end

# IP address tracking should not raise validation exceptions
context "with an invalid order" do
before { allow(order).to receive(:valid?).and_return(false) }

it "updates the IP address" do
expect(subject).to change(order, :last_ip_address).to(ip_address)
expect { subject }.to change(order, :last_ip_address).to(ip_address)
end
end

context "with a new order" do
let(:order) { build(:order) }

it "updates the IP address" do
expect(subject).to change(order, :last_ip_address).to(ip_address)
expect { subject }.to change(order, :last_ip_address).to(ip_address)
end
end
end
Expand Down
40 changes: 15 additions & 25 deletions core/spec/models/spree/taxon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,72 +115,62 @@
let(:taxon2_child) { create(:taxon, name: 't2_child', taxonomy: taxonomy, parent: taxon2) }

context "changing parent" do
subject do
-> { taxon2.update!(parent: taxon1) }
end
subject { taxon2.update!(parent: taxon1) }

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/t1/t2')
expect { subject }.to change{ taxon2.reload.permalink }.from('t/t2').to('t/t1/t2')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/t1/t2/t2_child')
expect { subject }.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/t1/t2/t2_child')
end
end

context "changing own permalink" do
subject do
-> { taxon2.update!(permalink: 'foo') }
end
subject { taxon2.update!(permalink: 'foo') }

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/foo')
expect { subject }.to change{ taxon2.reload.permalink }.from('t/t2').to('t/foo')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/foo/t2_child')
expect { subject }.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/foo/t2_child')
end
end

context "changing own permalink part" do
subject do
-> { taxon2.update!(permalink_part: 'foo') }
end
subject { taxon2.update!(permalink_part: 'foo') }

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/foo')
expect { subject }.to change{ taxon2.reload.permalink }.from('t/t2').to('t/foo')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/foo/t2_child')
expect { subject }.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/foo/t2_child')
end
end

context "changing parent and own permalink" do
subject do
-> { taxon2.update!(parent: taxon1, permalink: 'foo') }
end
subject { taxon2.update!(parent: taxon1, permalink: 'foo') }

it "changes own permalink" do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/t1/foo')
expect { subject }.to change{ taxon2.reload.permalink }.from('t/t2').to('t/t1/foo')
end

it "changes child's permalink" do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/t1/foo/t2_child')
expect { subject }.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/t1/foo/t2_child')
end
end

context 'changing parent permalink with special characters ' do
subject do
-> { taxon2.update!(permalink: 'spécial&charactèrs') }
end
subject { taxon2.update!(permalink: 'spécial&charactèrs') }

it 'changes own permalink with parameterized characters' do
is_expected.to change{ taxon2.reload.permalink }.from('t/t2').to('t/special-characters')
expect { subject }.to change{ taxon2.reload.permalink }.from('t/t2').to('t/special-characters')
end

it 'changes child permalink with parameterized characters' do
is_expected.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/special-characters/t2_child')
expect { subject }.to change{ taxon2_child.reload.permalink }.from('t/t2/t2_child').to('t/special-characters/t2_child')
end
end
end
Expand Down