Created
January 24, 2009 00:36
-
-
Save xaviershay/51288 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
| # Disclaimer | |
| <<-EOS | |
| This still just a crazy idea and I have not yet implemented it | |
| Not for long though | |
| Soon we take over the world | |
| EOS | |
| # Synopsis | |
| test(object, method, | |
| setup => expected_result, | |
| setup => { | |
| setup => expected_result | |
| } | |
| ) | |
| <<-EOS | |
| mocking is with RR | |
| method, setup, expected_result always have to_proc called on them if they respond_to? | |
| Symbol#to_proc features heavily in the following examples | |
| All code is data, so you can write 'macros' easily | |
| Emphasis is on tight examples - these are focused unit tests for your methods | |
| Integration tests are still vital, use your tool of choice | |
| Yes, they go inline next to your code | |
| Fold with your IDE | |
| EOS | |
| class Event < ActiveRecord::Base | |
| # Examples | |
| test(Event, :before_create, | |
| L{} => L{|x| x.include?(:before_create) } | |
| ) | |
| before_create :set_defaults | |
| test(Event.new, :set_defaults, | |
| :registrations_close_at => 3.months_from_now, | |
| :bank_name => '', | |
| :bank_account_number => '', | |
| :bank_bsb => '', | |
| L{|x| x.registrations_close_at = 1.month.from_now } => L{|x| x.registrations_close_at == 1.month.from_now}, | |
| L{|x| x.bank_name = "Westpac" } => L{|x| x.bank_name == "Westpac"}, | |
| L{|x| x.bank_account_number = "123456" } => L{|x| x.bank_account_number == "123456"}, | |
| L{|x| x.bank_bsb = "123456" } => L{|x| x.bank_bsb == "123456" } | |
| ) | |
| def set_defaults | |
| self.registrations_close_at ||= 3.months.from_now | |
| self.bank_name ||= '' | |
| self.bank_account_number ||= '' | |
| self.bank_bsb ||= '' | |
| end | |
| test(Event.new, :totals, | |
| L{|x| mock(EventStatistics).new(x) {true}} => true | |
| ) | |
| def totals | |
| EventStatistics.new(self) | |
| end | |
| test(Event.new, :accepting_registrations?, | |
| L{|x| mock(x).passes.size { 0 }} => false, | |
| L{|x| mock(x).passes.size { 1 }} => { | |
| L{|x| x.registrations_close_at = 1.second.from_now } => true, | |
| L{|x| x.registrations_close_at = Time.zone.now } => false | |
| } | |
| ) | |
| def accepting_registrations? | |
| self.passes.size > 0 && Time.zone.now < self.registrations_close_at | |
| end | |
| # Using local helpers | |
| check_attributes = L{|attributes| attributes.inject({}) {|a, v| | |
| a.update(L{|x| x.send("#{attribute}=", value) } => L{|x| x.send(attribute) == value }) | |
| }} | |
| test(Event.new, :set_defaults, { | |
| :registrations_close_at => 3.months_from_now, | |
| :bank_name => '', | |
| :bank_account_number => '', | |
| :bank_bsb => '' }.merge( | |
| check_attributes[ | |
| :registrations_close_at => 1.month_from_now, | |
| :bank_name => "Westpac", | |
| :bank_account_number => "123456", | |
| :bank_bsb => "123456" | |
| ]) | |
| ) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment