/
tmdb_handler_spec.rb
68 lines (59 loc) · 2.35 KB
/
tmdb_handler_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'rails_helper'
RSpec.describe TmdbHandler, type: :module do
let(:movie) { create(:movie_in_tmdb) }
let(:tmdb_id) { movie.tmdb_id }
subject { TmdbHandler.tmdb_handler_update_movie(movie) }
context 'with a valid movie' do
it 'returns true' do
VCR.use_cassette('tmdb_handler_update_movie_with_a_valid_movie', record: :new_episodes) do
expect(subject).to eq(true)
end
end
it 'updates the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_a_valid_movie', record: :new_episodes) do
expect{ subject }.to change{ movie.reload.updated_at }
end
end
end
context 'when no movie found from api response' do
before { movie.update(tmdb_id: 'wrong')}
it 'raises an error and does not update the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_an_invalid_movie', record: :new_episodes) do
expect{subject}.to raise_error(TmdbHandler::TmdbHandlerError).and not_change{ movie.reload.updated_at }
end
end
end
context 'when the title does not match' do
before { movie.update(title: 'Fargowrong') }
it 'does not raise an error' do
VCR.use_cassette('tmdb_handler_update_movie_with_wrong_title', record: :new_episodes) do
expect{subject}.not_to raise_error
end
end
it 'still updates the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_wrong_title', record: :new_episodes) do
expect{ subject }.to change{ movie.reload.updated_at }
end
end
end
context 'with outdated info' do
let(:wrong_mpaa_rating) { 'G' }
let(:correct_mpaa_rating) { 'R' }
before { movie.update(mpaa_rating: wrong_mpaa_rating) }
it 'updates movie with latest tmdb info' do
VCR.use_cassette('tmdb_handler_update_movie_with_outdated_info', record: :new_episodes) do
subject
expect(movie.reload.mpaa_rating).to eq(correct_mpaa_rating)
end
end
end
xcontext 'when the movie fails to save' do
let(:dupe_movie) { build(:movie, tmdb_id: movie.tmdb_id) }
before { dupe_movie.save(validate: false) }
it 'raises an error and does not update the movie' do
VCR.use_cassette('tmdb_handler_update_movie_with_a_valid_movie', record: :new_episodes) do
expect{subject}.to raise_error(TmdbHandler::TmdbHandlerError).and not_change{ movie.reload.updated_at }
end
end
end
end