Why RSpec throws an InvalidSignature error?

Viewed 779

I am new to Ruby, Rails and TDD, and I'm stuck during a test in my code. I'm trying to test the creation of a Recipe model, posted by a User created with FactoryBot.

My Recipe model (app/models/recipe.rb) is:

class Recipe < ActiveRecord::Base
  resourcify

  validates :image, presence: true
  validates :title, presence: true
  validates :preparazione, presence: true

  belongs_to :user
  has_many :likes
  has_many :comments

  has_one_attached :image
end

In spec/models/recipe_spec.rb I have:

require "rails_helper"

RSpec.describe Recipe, type: :model do
  describe "Creating a Recipe" do
    it "should be permitted" do
  
      @user = FactoryBot.create(:user)
      recipe = Recipe.new(title: 'Recipe', 
                          preparazione: 'Preparation',
                          image: '../support/test_image.jpg',
                          user_id: @user.id,
                          n_likes: 0,
                          n_comments: 0,
                          created_at: Time.now.utc)
      expect(recipe).to be_valid
      @user.destroy
    end
  end
end

This test fails, and the error is:

Failures:

1) Recipe Creating a Recipe should be permitted
 Failure/Error: expect(recipe).to be_valid

 ActiveSupport::MessageVerifier::InvalidSignature:
   ActiveSupport::MessageVerifier::InvalidSignature
 # ./spec/models/recipe_spec.rb:16:in `block (3 levels) in <top (required)>'

 Finished in 0.35273 seconds (files took 7.93 seconds to load)
 7 examples, 1 failure

 Failed examples:

 rspec ./spec/models/recipe_spec.rb:5 # Recipe Creating a Recipe should be permitted

Why is this error showing up and what does it mean?

1 Answers
recipe = Recipe.new(title: 'Recipe', 
                          preparazione: 'Preparation',
                          image: Rack::Test::UploadedFile.new('spec/support/test_image.jpg', 'image/jpg')), 
                          user_id: @user.id,
                          n_likes: 0,
                          n_comments: 0,
                          created_at: Time.now.utc)

Upload attachments using Rack::Test::UploadedFile class in Rspec.

Related