Last active
December 29, 2015 12:59
-
-
Save ichilton/7674331 to your computer and use it in GitHub Desktop.
rspec ordering problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This doesn't seem to work because the before(:each) is not called before the inner before(:all) is run... | |
context "when updating" do | |
before(:each) { @bible = create(:bible, code: 'OLD_CODE', alias: 'OLD_ALIAS') } | |
context "when not updating alias" do | |
before(:all) do | |
@bible.update_attributes(name: 'Different Name', code: 'UPDATED') | |
end | |
it "should have the updated title" do | |
expect(@bible.name).to eq 'Different Name' | |
end | |
it "should have the updated code" do | |
expect(@bible.code).to eq 'UPDATED' | |
end | |
it "should have the old alias" do | |
expect(@bible.alias).to eq 'OLD_ALIAS' | |
end | |
end | |
context "when updating alias" do | |
before(:all) do | |
bible.update_attributes(name: 'Different Name', code: 'UPDATED', alias: 'NEW_ALIAS') | |
end | |
it "should have the updated title" do | |
expect(bible.name).to eq 'Different Name' | |
end | |
it "should have the updated code" do | |
expect(bible.code).to eq 'UPDATED' | |
end | |
it "should have the old alias" do | |
expect(bible.alias).to eq 'NEW_ALIAS' | |
end | |
end | |
context "when updating alias to nil" do | |
before(:all) do | |
bible.update_attributes(name: 'Different Name', code: 'UPDATED', alias: nil) | |
end | |
it "should have the updated title" do | |
expect(bible.name).to eq 'Different Name' | |
end | |
it "should have the updated code" do | |
expect(bible.code).to eq 'UPDATED' | |
end | |
it "should have the correct alias (same as code)" do | |
expect(bible.alias).to eq 'UPDATED' | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Changing the before(:each) to let becomes: | |
context "when updating" do | |
let(:bible) { create(:bible, code: 'OLD_CODE', alias: 'OLD_ALIAS') } | |
context "when not updating alias" do | |
before(:all) do | |
bible.update_attributes(name: 'Different Name', code: 'UPDATED') | |
end | |
it "should have the updated title" do | |
expect(bible.name).to eq 'Different Name' | |
end | |
it "should have the updated code" do | |
expect(bible.code).to eq 'UPDATED' | |
end | |
it "should have the old alias" do | |
expect(bible.alias).to eq 'OLD_ALIAS' | |
end | |
end | |
context "when updating alias" do | |
before(:all) do | |
bible.update_attributes(name: 'Different Name', code: 'UPDATED', alias: 'NEW_ALIAS') | |
end | |
it "should have the updated title" do | |
expect(bible.name).to eq 'Different Name' | |
end | |
it "should have the updated code" do | |
expect(bible.code).to eq 'UPDATED' | |
end | |
it "should have the old alias" do | |
expect(bible.alias).to eq 'NEW_ALIAS' | |
end | |
end | |
context "when updating alias to nil" do | |
before(:all) do | |
bible.update_attributes(name: 'Different Name', code: 'UPDATED', alias: nil) | |
end | |
it "should have the updated title" do | |
expect(bible.name).to eq 'Different Name' | |
end | |
it "should have the updated code" do | |
expect(bible.code).to eq 'UPDATED' | |
end | |
it "should have the correct alias (same as code)" do | |
expect(bible.alias).to eq 'UPDATED' | |
end | |
end | |
end | |
# But this gives: | |
WARNING: let declaration `bible` accessed in a `before(:all)` hook at: | |
/vagrant/myproj_v2/spec/models/bible_spec.rb:140:in `block (5 levels) in <top (required)>' | |
This is deprecated behavior that will not be supported in RSpec 3. | |
`let` and `subject` declarations are not intended to be called | |
in a `before(:all)` hook, as they exist to define state that | |
is reset between each example, while `before(:all)` exists to | |
define state that is shared across examples in an example group. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks Philip.
That does look a lot neater/shorter, but the problem with subject is it works like before(:each) and let in that it runs for each test - that's why i've started doing things like:
The shorthand its syntax does look better in the specs, but does make the output a bit ambiguous as all you see is the test data, so I tend to use that more for things like be_valid....
Thanks,
Ian