Last active
December 11, 2015 16:19
-
-
Save wojtekmach/4627267 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # the point is to run the costly operation just once yet still be able to use | |
| # convenient "should change by" syntax. | |
| def transform | |
| # sleep 2 | |
| $x = 11 | |
| $y = 22 | |
| $z = 33 | |
| end | |
| describe 'transform' do | |
| before do | |
| $x = 1 | |
| $y = 2 | |
| $z = 3 | |
| end | |
| context 'redundant' do | |
| it do | |
| $x.should == 1 | |
| $y.should == 2 | |
| $z.should == 3 | |
| transform | |
| $x.should == 11 | |
| $y.should == 22 | |
| $z.should == 33 | |
| end | |
| end | |
| context 'slow' do | |
| it { expect { transform }.to change { $x }.by(10) } | |
| it { expect { transform }.to change { $y }.by(20) } | |
| it { expect { transform }.to change { $z }.by(30) } | |
| end | |
| context 'ugly' do | |
| it do | |
| expect do | |
| expect do | |
| expect do | |
| transform | |
| end.to change { $x }.by(10) | |
| end.to change { $y }.by(20) | |
| end.to change { $z }.by(30) | |
| end | |
| end | |
| context 'change chain' do | |
| it do | |
| ch = ChangeChain.new { transform } | |
| ch.should change { $x }.by(10) | |
| ch.should change { $y }.by(20) | |
| ch.should change { $z }.by(30) | |
| ch.assert | |
| end | |
| end | |
| context 'change chain 2' do | |
| it do | |
| change_chain -> { transform }, | |
| change { $x }.by(10), | |
| change { $y }.by(20), | |
| change { $z }.by(30) | |
| end | |
| end | |
| context 'perfect' do | |
| it do | |
| pending '???' | |
| end | |
| end | |
| end | |
| class ChangeChain | |
| def initialize(&block) | |
| @last_block = block | |
| end | |
| def should(change) | |
| last_block = @last_block | |
| @last_block = -> { -> { last_block.call }.should change } | |
| end | |
| def assert | |
| @last_block.call | |
| end | |
| end | |
| def change_chain(block, *changes) | |
| ch = ChangeChain.new(&block) | |
| changes.each { |change| ch.should change } | |
| ch.assert | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment