Rails Polymorphic POST test failing

45 Views Asked by At

I have a polymorphic model Note:

class Note < ApplicationRecord
    belongs_to :commentor, class_name: "User"
    belongs_to :notable, polymorphic: true, touch: true
    belongs_to :program
    belongs_to :organization

    validates :commentor, presence: true, uniqueness: { scope: [:notable_id, :notable_type], message: "can only create one note. Please edit your existing note" }
    validates :content, presence: true, length: { maximum: 150, message: "only 150 characters" }
    validates :program, presence: true
    validates :organization, presence: true
end

For the controllers I have it set up as follows: app/controllers/tools/workouts/notes_controller.rb

class Tools::Workouts::NotesController < Tools::NotesController
    before_action :set_routine


    private

    def set_routine
        @notable = current_program.workouts.find(params[:workout_id])
    end
end

Then: app/controllers/tools/notes_controller.rb

def create
    @note = @notable.notes.new(note_params)
    @note.commentor = current_user
    @note.organization = current_organization
    @note.program = current_program
    respond_to do |format|
        if @notable.save
            flash[:success] = t(".success")
            format.html { recede_or_redirect_to [:tools, :program, 
            @notable, program_id: current_program] }
            format.json { render :show, status: :created }
            format.turbo_stream { flash.now[:success] = t(".success") }
        else
            flash[:warning] = t(".error")
            format.html { render :new, status: :unprocessable_entity }
            format.json { render json: @notable.errors, status: 
             :unprocessable_entity }
            format.turbo_stream { flash.now[:warning] = t(".error") }
        end
    end
end

Routes:

    namespace :tools do
      resources :programs, only: %i[] do
        resources :notes
        resources :workouts do
          resources :notes, module: :workouts, except: %i[update destroy 
          edit index]
        end
      end
    end

I'm trying to test this action. This current set up works correctly when I play around with it in development. However writing a test for this seems to be a littler harder then it seems.

The test I'm having problem with:

test "should create note" do
    assert_difference("Note.count") do
        post polymorphic_url([:tools, current_program, @workout, notes]), params: { 
            note: {
                content: "Test workout note"
            }
        }
        puts request.POST.inspect
        puts polymorphic_url([:tools, current_program, @workout, notes])
    end
  
    assert_equal @workout.notes.last.content, "Test workout note"
    assert_equal @workout.notes.last.program, @program
    assert_equal @workout.notes.last.commentor, @user
    assert_equal @workout.notes.last.organization, @organization
    assert_equal @workout.notes.last.notable_type, "Workout"
end

The paths that are created are correct and the params, via the puts. But this test returns a fail with Note count not increasing, but statying the same.

How does one write test to test the create action for this?

0

There are 0 best solutions below