diff --git a/app/models/user.rb b/app/models/user.rb index ac83c4d70..36f43dc55 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -83,6 +83,7 @@ def feed WHERE follower_id = :user_id" Micropost.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id) + .includes(:user, image_attachment: :blob) end # Follows a user. diff --git a/test/integration/microposts_interface_test.rb b/test/integration/microposts_interface_test.rb index b90cffaa5..c76662363 100644 --- a/test/integration/microposts_interface_test.rb +++ b/test/integration/microposts_interface_test.rb @@ -2,10 +2,35 @@ class MicropostsInterfaceTest < ActionDispatch::IntegrationTest + class QueryCountHandler + attr_reader :count + + def initialize + @count = 0 + end + + def call(name, started, finished, unique_id, payload) + # puts payload[:sql] + @count += 1 + end + end + def setup @user = users(:michael) end + def count_sql(message) + warn "Counting SQL queries for: #{message}" + + count_handler = QueryCountHandler.new + ActiveSupport::Notifications.subscribe 'sql.active_record', count_handler + + yield + + warn "Observed #{count_handler.count} SQL queries" + ActiveSupport::Notifications.unsubscribe count_handler + end + test "micropost interface" do log_in_as(@user) get root_path @@ -18,12 +43,18 @@ def setup # Valid submission content = "This micropost really ties the room together" image = fixture_file_upload('kitten.jpg', 'image/jpeg') + assert_difference 'Micropost.count', 1 do - post microposts_path, params: { micropost: { content: content, - image: image } } + count_sql 'Create a valid post' do + post microposts_path, params: { micropost: { content: content, + image: image } } + end + assert assigns(:micropost).image.attached? + end + + count_sql 'Follow redirect after posting' do + follow_redirect! end - assert assigns(:micropost).image.attached? - follow_redirect! assert_match content, response.body # Delete a post. assert_select 'a', 'delete'