简体   繁体   中英

Rails and RSpec: ActionController::UnknownFormat error

I have been trying to test Paperclip file uploads using RSpec with Rails.

When I run the server, I have no problems, but when I run my (very basic) RSpec test, I receive the following error:

 Failure/Error: visit root_path

 ActionController::UnknownFormat:
   PhotosController#index is missing a template for this request format and variant.

   request.formats: ["text/html"]
   request.variant: []

Here is my test:

require 'rails_helper'

    feature 'photos' do

      context 'no photos have been added' do

        scenario 'should display prompt to add a photo' do
          visit root_path
          expect(page).to have_content 'No photos yet'
          expect(page).to have_link 'Add a photo'
        end

      end

    end

Here is my view in haml:

- if @photos.any?
  - @photos.each do |photo|
    = image_tag photo.image
    = photo.caption
- else
  %h1 No photos yet


%h2= link_to "Add a photo", new_photo_path

Here is my photo model:

class Photo < ApplicationRecord
  validates :image, presence: true

  has_attached_file :image, styles: { :medium => "640x" }
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end

I had some problems with a NoMethodError in my test saying that has_attached_file was unrecognized, but after Googling extensively I found someone had luck by adding a paperclip.rb file to config/initializers with the following:

require "paperclip/railtie"

Paperclip::Railtie.insert

Any idea why I might be experiencing this issue, and how I might overcome it?

The error is telling you it can't find the template handler for the view. Since you're using Haml as the handler, you have to add the haml gem to your gem file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM