Skip to content

Instantly share code, notes, and snippets.

@levi
Created October 30, 2010 01:32
Show Gist options
  • Save levi/654784 to your computer and use it in GitHub Desktop.
Save levi/654784 to your computer and use it in GitHub Desktop.
A really smelly, but working way to mock test Active Record chained queries in Rails 3.
# @session and @mock_project instance variables are defined elsewhere.
before(:each) do
joins = mock(ActiveRecord::Relation)
ProjectUser.should_receive(:joins).with(:project_user_datum).and_return(joins)
joins.should_receive(:where).
with('project_user_data.project_id = :project_id', { :project_id => @mock_project }).
and_return(@mock_project)
get :index, {}, @session
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment