|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class VotesControllerTest < ActionDispatch::IntegrationTest |
| 4 | + setup do |
| 5 | + @movie_without_votes = movies(:three) |
| 6 | + post session_url, params: { email_address: users(:one).email_address, password: "password" } |
| 7 | + end |
| 8 | + |
| 9 | + test "should create vote" do |
| 10 | + assert_difference -> { @movie_without_votes.votes.count } do |
| 11 | + post movie_votes_url(@movie_without_votes), params: { vote: { vote_type: "like" } } |
| 12 | + end |
| 13 | + |
| 14 | + assert_redirected_to movie_url(@movie_without_votes) |
| 15 | + assert_equal "Vote was successfully created.", flash[:notice] |
| 16 | + end |
| 17 | + |
| 18 | + test "should update an exsting vote" do |
| 19 | + movie = movie_votes(:one).movie |
| 20 | + assert_no_difference -> { movie.votes.count } do |
| 21 | + post movie_votes_url(movie), params: { vote: { vote_type: "dislike" } } |
| 22 | + end |
| 23 | + assert_redirected_to movie_url(movie) |
| 24 | + assert_equal "Vote was successfully created.", flash[:notice] |
| 25 | + assert_equal movie_votes(:one).vote_type, "dislike" |
| 26 | + end |
| 27 | + |
| 28 | + test "should not create vote without type" do |
| 29 | + assert_no_difference -> { @movie_without_votes.votes.count } do |
| 30 | + post movie_votes_url(@movie_without_votes), params: { vote: { vote_type: nil } } |
| 31 | + end |
| 32 | + assert_redirected_to movie_url(@movie_without_votes) |
| 33 | + assert_equal "Failed to create vote.", flash[:alert] |
| 34 | + end |
| 35 | + |
| 36 | + test "should not create vote with invalid type" do |
| 37 | + assert_no_difference -> { @movie_without_votes.votes.count } do |
| 38 | + post movie_votes_url(@movie_without_votes), params: { vote: { vote_type: "invalid" } } |
| 39 | + end |
| 40 | + |
| 41 | + assert_redirected_to movie_url(@movie_without_votes) |
| 42 | + assert_equal "Failed to create vote.", flash[:alert] |
| 43 | + end |
| 44 | + |
| 45 | + test "should destroy vote" do |
| 46 | + movie = movies(:two) |
| 47 | + |
| 48 | + assert_difference -> { movie.votes.count }, -1 do |
| 49 | + delete movie_vote_path(movie, movie.votes(:one).id) |
| 50 | + end |
| 51 | + |
| 52 | + assert_redirected_to movie_url(movie) |
| 53 | + assert_equal "Vote was successfully deleted.", flash[:notice] |
| 54 | + end |
| 55 | +end |
0 commit comments