在控制器测试中使用通过 Active Storage 上传的图像创建对象时遇到问题

Trouble creating object with image uploaded via Active Storage in controller testing

所以我尝试在我的 Rails 应用程序中测试控制器,但出现以下错误:

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>'

我的食谱控制器测试:

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