require "test_helper" class ScoresControllerTest < ActionDispatch::IntegrationTest setup do @tartiflette = Tartiflette.create!(scoring_id: 1) @criterium = ScoringCriterium.create!(name: "Taste", category: "Flavor") @score = Score.create!(tartiflette: @tartiflette, scoring_criterium: @criterium, value: 4) end test "should get new score form" do get new_tartiflette_score_path(@tartiflette) assert_response :success assert_select "form" assert_select "select[name=?]", "scores[#{@criterium.id}][value]" end test "should create scores for tartiflette" do assert_difference("Score.count", 1) do post tartiflette_scores_path(@tartiflette), params: { scores: { @criterium.id => { value: 5 } } } end assert_redirected_to root_path end # test "should not create scores if already scored" do # session = { :scored_tartiflettes => [ @tartiflette.id ] } # assert_no_difference("Score.count") do # post tartiflette_scores_path(@tartiflette), params: { # scores: { @criterium.id => { value: 5 } } # } # end # assert_redirected_to root_path # assert_match /Vous avez déja noté cette tartiflette/, flash[:alert] # end test "should not create scores with invalid data" do assert_no_difference("Score.count") do post tartiflette_scores_path(@tartiflette), params: { scores: { @criterium.id => { value: nil } } } end assert_response :unprocessable_entity end test "should get edit scores form" do get tartiflette_edit_scores_path(@tartiflette) assert_response :success assert_select "form" assert_select "select[name=?]", "scores[#{@criterium.id}][value]" end test "should update scores for tartiflette" do patch tartiflette_update_scores_path(@tartiflette), params: { scores: { @score.id => { value: 3 } } } assert_redirected_to root_path @score.reload assert_equal 3, @score.value end # test "should not update scores with invalid data" do # patch tartiflette_update_scores_path(@tartiflette), params: { # scores: { @score.id => { value: nil } } # } # @score.reload # assert_not_equal nil, @score.value # assert_redirected_to tartiflette_edit_scores_path(@tartiflette) # end end