I need to test that a piece of code executes two sql statements, which I’m doing by saying
ActiveRecord::Base.connection.should_receive(:execute).with("s1")
ActiveRecord::Base.connection.should_receive(:execute).with("s2")
However, the code also executes a lot of other statements that I don’t care about, which trips up the test. How do I tell Rspec to make sure that s1 and s2 are in the list of executed statements?
Update your version of RSpec to 2.12 and you will have access to the
and_call_originalmethod (see the documentation and use cases). Using that method, you can stub theexecutemethod ofActiveRecord::Base.connectionand make it call through to the original method, and then just add to that the expectations you want:If for whatever reason you are not using (or don’t want to use) the latest version of RSpec, you can achieve the same functionality this way:
Refs: