[ruby-on-rails] rspec-rails를 사용하여 파일 업로드 테스트

레일에서 파일 업로드를 테스트하고 싶지만 어떻게해야하는지 잘 모르겠습니다.

컨트롤러 코드는 다음과 같습니다.

def uploadLicense
    #Create the license object
    @license = License.create(params[:license])


    #Get Session ID
    sessid = session[:session_id]

    puts "\n\nSession_id:\n#{sessid}\n"

    #Generate a random string
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(5) { |i| newpass << chars[rand(chars.size-1)] }

    #Get the original file name
    upload=params[:upload]
    name =  upload['datafile'].original_filename

    @license.format = File.extname(name)

    #calculate license ID and location
    @license.location = './public/licenses/' + sessid + newpass + name

    #Save the license file
    #Fileupload.save(params[:upload], @license.location) 
    File.open(@license.location, "wb") { |f| f.write(upload['datafile'].read) }

     #Set license ID
    @license.license_id = sessid + newpass

    #Save the license
    @license.save

    redirect_to :action => 'show', :id => @license.id
end

이 사양을 시도했지만 작동하지 않습니다.

it "can upload a license and download a license" do
    file = File.new(Rails.root + 'app/controllers/lic.xml')
    license = HashWithIndifferentAccess.new
    license[:datafile] = file
    info = {:id => 4}
    post :uploadLicense, {:license => info, :upload => license}
end

rspec을 사용하여 파일 업로드를 어떻게 시뮬레이트 할 수 있습니까?



답변

fixture_file_upload 메소드를 사용 하여 파일 업로드를 테스트 할 수 있습니다 . 테스트 파일을 “{Rails.root} / spec / fixtures / files” 디렉토리에 두십시오.

before :each do
  @file = fixture_file_upload('files/test_lic.xml', 'text/xml')
end

it "can upload a license" do
  post :uploadLicense, :upload => @file
  response.should be_success
end

params [ ‘upload’] [ ‘datafile’] 형식의 파일을 예상 한 경우

it "can upload a license" do
  file = Hash.new
  file['datafile'] = @file
  post :uploadLicense, :upload => file
  response.should be_success
end


답변

RSpec 만 사용하여 파일 업로드를 테스트 할 수 있는지 잘 모르겠습니다. 당신은 시도 카피 바라를 ?

attach_file요청 사양에서 capybara의 방법을 사용하여 파일 업로드를 쉽게 테스트 할 수 있습니다.

예를 들어 (이 코드는 데모 전용) :

it "can upload a license" do
  visit upload_license_path
  attach_file "uploadLicense", /path/to/file/to/upload
  click_button "Upload License"
end

it "can download an uploaded license" do
  visit license_path
  click_link "Download Uploaded License"
  page.should have_content("Uploaded License")
end


답변

Rack :: Test *를 포함하는 경우 간단히 테스트 방법을 포함하십시오.

describe "my test set" do
  include Rack::Test::Methods

그런 다음 UploadedFile 메서드를 사용할 수 있습니다.

post "/upload/", "file" => Rack::Test::UploadedFile.new("path/to/file.ext", "mime/type")

* 참고 :이 예는 랙을 확장하는 Sinatra를 기반으로하지만 Rack, TTBOMK를 사용하는 Rails 와도 작동해야합니다.


답변

RSpec을 사용 하여이 작업을 수행하지는 않았지만 사진 업로드와 비슷한 작업을 수행하는 Test :: Unit 테스트가 있습니다. 다음과 같이 업로드 된 파일을 ActionDispatch :: Http :: UploadedFile의 인스턴스로 설정했습니다.

test "should create photo" do
  setup_file_upload
  assert_difference('Photo.count') do
    post :create, :photo => @photo.attributes
  end
  assert_redirected_to photo_path(assigns(:photo))
end


def setup_file_upload
  test_photo = ActionDispatch::Http::UploadedFile.new({
    :filename => 'test_photo_1.jpg',
    :type => 'image/jpeg',
    :tempfile => File.new("#{Rails.root}/test/fixtures/files/test_photo_1.jpg")
  })
  @photo = Photo.new(
    :title => 'Uploaded photo',
    :description => 'Uploaded photo description',
    :filename => test_photo,
    :public => true)
end

비슷한 것도 당신을 위해 작동 할 수 있습니다.


답변

이것은 내가했던 방법입니다 Rails 6, RSpec그리고Rack::Test::UploadedFile

describe 'POST /create' do
  it 'responds with success' do
    post :create, params: {
      license: {
        picture: Rack::Test::UploadedFile.new("#{Rails.root}/spec/fixtures/test-pic.png"),
        name: 'test'
      }
    }

    expect(response).to be_successful
  end
end

포함ActionDispatch::TestProcess 하는 내용이 확실하지 않은 경우 포함 하거나 다른 코드를 포함하지 마십시오 .


답변

작동시키기 위해서는 다음 두 가지를 모두 추가해야했습니다.

describe "my test set" do
  include Rack::Test::Methods
  include ActionDispatch::TestProcess


답변