Interesting RSpec Tips
This afternoon I found a set of tests in the code that weren't implemented, and should be. They were stubbed out by one of the guys on the Team, but he didn't have time to really implement them, he just wanted to remind himself that we needed these tests, and so he stubbed them out and then went on about what he needed to do. I noticed them, and decided that since I didn't have a lot going on at this time, it made sense to give it a go at implementing the tests.
After all, I know there's a lot about rspec I don't know, and this would be a nice way to learn about it.
Some of the tests were really pretty clear: make sure the main routine returns something. So how to do that - simply? Well… we can always stub out the methods with simple return values and then just make sure that you get back what you expect.
require 'lead_assignment/entry_point' describe LeadAssignment::EntryPoint do describe ".unassign_and_assign" do before(:each) do class FauxRepClient def get_reps(division) [] end end LeadAssignment::EntryPoint.stub(:reps_client => FauxRepClient.new) LeadAssignment::EntryPoint.stub(:fetch_accounts => []) LeadAssignment::EntryPoint.stub(:add_accounts => []) end
on this first test, I noticed that I wanted to start all my tests with this little configuration, so it was easy to put into a before() block and then it was going to be done before each test within the scope of the enclosing describe. That's nice to remember.
Then I can do the end-to-end test:
it "returns a result" do LeadAssignment::EntryPoint.stub(:sink => nil) results = LeadAssignment::EntryPoint.unassign_and_reassign('cleveland') results.should == { :unassignments => [], :assignments => [] } end
and my first test is done!
I learned a lot about testing writing that, and it was going to pay off as I did the others. They all looked about the same - you stub out certain methods, run certain sections, and then check the output. Not bad at all. Just need to be careful and methodical about what you're doing.
Then I came to a more challenging problem: I needed to know when a specific instance was being called, and with a certain set of arguments. That's not too bad - you have to make the instance, and then you can use it:
it "writes the results to salesforce" do class FauxSFClient def bulk_store(accounts) nil end end my_store = FauxSFStore.new LeadAssignment::EntryPoint.stub(:send_assignments_to_sf? => true) LeadAssignment::EntryPoint.stub(:store => my_store) my_store.should_receive(:bulk_store).with([]).exactly(2).times LeadAssignment::EntryPoint.unassign_and_reassign('cleveland') end
and again, this works great! I like that I can specify the args to the method, and the instance doesn't need to be exactly what's in the code - my faux class is just as good for this as anything.
The final trick I learned has to do with passing blocks to methods and testing the contents of those blocks. You can't actually tell what's in a block, but you can evaluate it and then test that value:
it "writes log messages properly for summary script" do LeadAssignment::EntryPoint.stub(:send_assignments_to_sf? => false) LeadAssignment::EntryPoint.stub(:unassign => []) LeadAssignment::EntryPoint.stub(:assign => []) LeadAssignment::EntryPoint.stub(:sink => nil) QuantumLead::Application.logger.should_receive(:info) do |method, &block| method.should == "LeadAssignment::EntryPoint.unassign_and_reassign" block.call.should == "Starting LeadAssignment in cleveland" end LeadAssignment::EntryPoint.unassign_and_reassign('cleveland') end
and you can have as many of those logger checking blocks as you believe you will have calls to the logger. It's pretty nice.
With all this in place, I was able to whip up the necessary tests for the code in short order. Pretty nice tools.