mercredi 11 décembre 2019

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

So I am trying to test controllers in my Rails app but I get a following error:

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

My 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

I tested everything in the browser, everything works, it's just the testing environment I'm having troubles with.

What I realised is that the params differ when creating an object through a form and in the test:

a) Through testing (not working)

{"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) Through form (working)

{"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>}

I am using devise for user authentication and cocoon for nested forms.

Aucun commentaire:

Enregistrer un commentaire