ruby-on-rails - 在 Controller 测试中使用通过 Active Storage 上传的图像创建对象时遇到问题

标签 ruby-on-rails testing image-uploading rails-activestorage

因此,我尝试在我的 Rails 应用程序中测试 Controller ,但出现以下错误:

RecipesControllerTest#test_should_create_recipe:
ActiveSupport::MessageVerifier::InvalidSignature: ActiveSupport::MessageVerifier::InvalidSignature
    app/controllers/recipes_controller.rb:31:in `create'
    test/controllers/recipes_controller_test.rb:25:in `block (2 levels) in <class:RecipesControllerTest>'
    test/controllers/recipes_controller_test.rb:24:in `block in <class:RecipesControllerTest>'

我的食谱 Controller 测试:

setup do
    @user = users(:one)
    sign_in @user
    @recipe = recipes(:one)
    @recipe.image.attach(io: File.open('app/assets/images/vegan-chilli.jpg'), filename: 'vegan-chilli.jpg', content_type: 'image/jpg')
  end

test "should create recipe" do
    assert_difference('Recipe.count') do
      post recipes_url, params: { recipe: { description: @recipe.description, title: @recipe.title, user_id: @recipe.user_id, image: @recipe.image,
         ingredients_attributes: [{ name: 'Ingredient 1'}], directions_attributes: [{ step: 'Step 1'}] } }
    end
    assert_redirected_to recipe_url(Recipe.last)
  end

我在浏览器中测试了一切,一切正常,这只是我遇到问题的测试环境。

我意识到在通过表单创建对象时和在测试中创建对象时参数不同:

a) 通过测试(不工作)

{"title"=>"Rainbow Pie", "description"=>"Gorgeous Pie!", "image"=>"#<ActiveStorage::Attached::One:0x000056126bb3d408>", "ingredients_attributes"=>[<ActionController::Parameters {"name"=>"Ingredient 1"} permitted: true>], "directions_attributes"=>[<ActionController::Parameters {"step"=>"Step 1"} permitted: true>]}

b) 通过表格(工作)

{"title"=>"Whate", "description"=>"asdadasasd", "image"=>#<ActionDispatch::Http::UploadedFile:0x00007f466840e678 @tempfile=#<Tempfile:/tmp/RackMultipart20191210-5380-7tk69z.jpg>, @original_filename="IMG_6111-2_male-600x900.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"recipe[image]\"; filename=\"IMG_6111-2_male-600x900.jpg\"\r\nContent-Type: image/jpeg\r\n">, "ingredients_attributes"=><ActionController::Parameters {"1576000953040"=><ActionController::Parameters {"name"=>"asadadsdsa", "_destroy"=>"false"} permitted: true>} permitted: true>, "directions_attributes"=><ActionController::Parameters {"1576000954677"=><ActionController::Parameters {"step"=>"sadafaf", "_destroy"=>"false"} permitted: true>} permitted: true>}

我正在使用 devise 进行用户身份验证,并使用 cocoon 进行嵌套表单。

最佳答案

尝试使用 fixture_file_upload apidock

检查这个SO answer还有。

setup do
    @user = users(:one)
    sign_in @user
    @recipe = recipes(:one)
    @recipe_image = fixture_file_uploade('app/assets/images/vegan-chilli.jpg', 'image/jpg')
  end

test "should create recipe" do
    assert_difference('Recipe.count') do
        post recipes_url, params: { 
            recipe: { 
              description: @recipe.description, title: @recipe.title, user_id: @recipe.user_id, 
              image: @recipe_image,
              ingredients_attributes: [{ name: 'Ingredient 1'}],           
              directions_attributes: [{ step: 'Step 1'}] 
            } 
        }
    end
    assert_redirected_to recipe_url(Recipe.last)
end

关于ruby-on-rails - 在 Controller 测试中使用通过 Active Storage 上传的图像创建对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59288438/

相关文章:

ruby-on-rails - 为什么这个 Rails Controller 测试失败了?

ruby-on-rails - 将更新推送到 Rails 中的页面

ruby-on-rails - 路由: resource_path or resource_url?

typescript - Jest/ Jasmine : Weird Execution Order Of beforeEach()

unit-testing - 只允许 Selenium 访问该页面

asp.net-mvc - CKEditor 图片上传

javascript - 在 Chartkick.js 中添加价格标签

Spring Boot 测试未使用测试属性

google-app-engine - 使用 Google App Engine 上传图片

django - 在 Django : when to use save() vs chunks() vs cleaned_data? 中处理图像上传表单