【问题标题】:Rails Integration testing with rspec and fixture_file_upload使用 rspec 和 fixture_file_upload 进行 Rails 集成测试
【发布时间】:2021-11-15 06:03:43
【问题描述】:

我最近在使用 rspec 进行集成测试时遇到了问题。

RSpec.describe "Images", type: :request do
  before(:each) do
    @user = User.create(username: "sam", password: "password")
    @tag = @user.tags.create(name: "outdoors")

    post login_path, params: { username: "sam", password: "password" }
    expect(session[:user_id]).to eql @user.id

    @file = fixture_file_upload("/dingo.jpeg", "image/jpeg")
  end

  it "can show images" do
    @image = @user.images.create(caption: "hello", image_file: @image)
    get user_image_path(@user, @image)
    expect(response).to be_success
  end
end

before each 创建一个有效的用户,标记并登录用户。然后它加载一个图像文件。 测试它自己为@user 创建一个新图像,并为它分配一个标题值,并为其 :image_file 加载文件。

Image 模型使用 has_one_attached :image_file 将上传的图像与图像记录相关联

class Image < ApplicationRecord
  belongs_to :user
  has_and_belongs_to_many :tags
  has_one_attached :image_file
end

控制器和动作如下:

class ImageController < ApplicationController
  before_action :unauthorized_redirect
  
  def show
    @user = current_user
    @image = Image.find(params[:id])
    @tags = @user.tags.all
    authorize @image, :show?
  end

  private

  def image_params
    params.require(:image).permit(:caption, :image_file, tag_ids: [])
  end
end

当我运行测试时出现错误:

Failures:

  1) Images can show images
     Failure/Error: <%= image_tag @image.image_file, class: "img-fluid show-image" %>
     
     ActionView::Template::Error:
       Can't resolve image into URL: undefined method `persisted?' for nil:NilClass
     # ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'
     # <internal:kernel>:90:in `tap'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/rendering.rb:20:in `with_renderer'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/actiontext-6.1.4.1/lib/action_text/engine.rb:59:in `block (4 levels) in <class:Engine>'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/tempfile_reaper.rb:15:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/etag.rb:27:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/conditional_get.rb:27:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/head.rb:12:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:266:in `context'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/session/abstract/id.rb:260:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:37:in `call_app'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `block in call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/rack/logger.rb:26:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/method_override.rb:24:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/runtime.rb:22:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-2.2.3/lib/rack/sendfile.rb:110:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/railties-6.1.4.1/lib/rails/engine.rb:539:in `call'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/mock_session.rb:29:in `request'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:266:in `process_request'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rack-test-1.1.0/lib/rack/test.rb:119:in `request'
     # /home/sam/.rvm/gems/ruby-3.0.0/gems/rails-controller-testing-1.0.5/lib/rails/controller/testing/integration.rb:16:in `block (2 levels) in <module:Integration>'
     # ./spec/requests/images_spec.rb:38:in `block (2 levels) in <main>'
     # ------------------
     # --- Caused by: ---
     # NoMethodError:
     #   undefined method `persisted?' for nil:NilClass
     #   ./app/views/image/show.html.erb:7:in `_app_views_image_show_html_erb__3727703739275822003_20580'

Finished in 0.79372 seconds (files took 0.46552 seconds to load)
30 examples, 1 failure

Failed examples:

rspec ./spec/requests/images_spec.rb:36 # Images can show images

我已经用断点调试了这个问题,但我只是不明白为什么它在生产中工作时在测试中不起作用。我希望我已经提供了足够的信息,这个错误在过去的两天里一直让我发疯。 谢谢!

版本: 导轨:6.1.4.1 rspec:5.0.2

【问题讨论】:

    标签: ruby-on-rails ruby rspec rspec-rails rails-activestorage


    【解决方案1】:

    更改了一行并修复了它

     it "can show images" do
        @image = @user.images.create(caption: "hello", image_file: @image)
        get user_image_path(@user, @image)
        *expect(response).to be_success ----REMOVE*
        expect(response.successful?).to eql true
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多