Created
August 28, 2011 00:59
-
-
Save blatyo/1176088 to your computer and use it in GitHub Desktop.
My comments are prepended with ACM
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
| # This is the method I am testing. | |
| class Log | |
| private | |
| class Parse | |
| @queue = :parse | |
| # This is a resque job. perform will ultimately be called asynchronously. | |
| # Therefore, must find the Log before performing the action because it | |
| # may be dirty or have been deleted since. | |
| def self.perform(log_id) | |
| # ACM: I would change | |
| # Log.find(log_id).parse if Log.exists?(conditions: {id: log_id}) | |
| # to | |
| log = Log.find(log_id) and log.parse | |
| # because it would require one query instead of two | |
| nil | |
| end | |
| end | |
| end | |
| # Here is my spec | |
| # resque/log_parse_spec.rb | |
| describe Log::Parse do | |
| context "when given a valid log id" do | |
| before(:each) do | |
| @log = create(:log) | |
| end | |
| it "should parse a log" do | |
| # ACM: This is a typical pattern used | |
| @log.should_receive(:parse).once | |
| Log.stub!(:find).and_return(@log) | |
| Log::Parse.perform(@log.id) | |
| end | |
| end | |
| context "when given a stale log id" do | |
| before(:each) do | |
| @log = create(:log) | |
| @log.delete | |
| end | |
| it "should not parse any instance of Log" do | |
| # ACM: I agree for this instance | |
| # Here is a valid usage of any_instance. | |
| Log.any_instance.should_not_receive(:parse) | |
| Log::Parse.perform(@log.id) | |
| end | |
| it "shouldn't raise an error if the log doesn't exist" do | |
| expect { Log::Parse.perform(@log.id) }.to_not raise_error | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment