|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "isolation/abstract_unit" |
| 4 | +require "rack/test" |
| 5 | +require "rails-dom-testing" |
| 6 | + |
| 7 | +module ApplicationTests |
| 8 | + class UploadsIntegrationTest < ActiveSupport::TestCase |
| 9 | + include ActiveSupport::Testing::Isolation |
| 10 | + include Rack::Test::Methods |
| 11 | + include Rails::Dom::Testing::Assertions |
| 12 | + |
| 13 | + self.file_fixture_path = "#{RAILS_FRAMEWORK_ROOT}/activestorage/test/fixtures/files" |
| 14 | + |
| 15 | + def setup |
| 16 | + build_app |
| 17 | + end |
| 18 | + |
| 19 | + def teardown |
| 20 | + teardown_app |
| 21 | + end |
| 22 | + |
| 23 | + def test_creating_new_upload |
| 24 | + rails "active_storage:install" |
| 25 | + |
| 26 | + rails "generate", "model", "user", "name:string", "avatar:attachment" |
| 27 | + rails "db:migrate" |
| 28 | + |
| 29 | + app_file "config/routes.rb", <<~RUBY |
| 30 | + Rails.application.routes.draw do |
| 31 | + resources :users, only: [:show, :create] |
| 32 | + end |
| 33 | + RUBY |
| 34 | + |
| 35 | + app_file "app/controllers/users_controller.rb", <<~RUBY |
| 36 | + class UsersController < ApplicationController |
| 37 | + def show |
| 38 | + @user = User.find(params[:id]) |
| 39 | + render :show |
| 40 | + end |
| 41 | +
|
| 42 | + def create |
| 43 | + @user = User.new(user_params) |
| 44 | +
|
| 45 | + if @user.save |
| 46 | + redirect_to user_url(@user), notice: "User was successfully created." |
| 47 | + else |
| 48 | + render :new, status: :unprocessable_entity |
| 49 | + end |
| 50 | + end |
| 51 | +
|
| 52 | + private |
| 53 | + def user_params |
| 54 | + params.require(:user).permit(:name, :avatar) |
| 55 | + end |
| 56 | + end |
| 57 | + RUBY |
| 58 | + |
| 59 | + app_file "app/views/users/show.html.erb", <<~ERB |
| 60 | + <p><%= @user.name %></p> |
| 61 | + <p><%= image_tag @user.avatar %></p> |
| 62 | + ERB |
| 63 | + |
| 64 | + app("development") |
| 65 | + |
| 66 | + post "/users", user: { name: "zomg", avatar: Rack::Test::UploadedFile.new(file_fixture("racecar.jpg")) } |
| 67 | + assert_equal 302, last_response.status |
| 68 | + |
| 69 | + get "/users/1" |
| 70 | + assert_equal 200, last_response.status |
| 71 | + assert_select "p", text: "zomg" |
| 72 | + assert_select "img", count: 1 |
| 73 | + end |
| 74 | + |
| 75 | + private |
| 76 | + def document_root_element |
| 77 | + Nokogiri::HTML5.parse(last_response.body) |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments