-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathad_controller_spec.rb
More file actions
139 lines (120 loc) · 5 KB
/
Copy pathad_controller_spec.rb
File metadata and controls
139 lines (120 loc) · 5 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe AdController, type: :controller do
describe 'POST #create' do
it 'with all should plus Ad_record' do
expect do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
end.to change(Ad, :count).by(1)
expect(response).to redirect_to(ad_index_path)
end
it "without text should render 'new'" do
post :create, params: { ad: { 'text' => nil,
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
expect(response).to render_template :new
end
it "without price should render 'new'" do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => nil,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
expect(response).to render_template :new
end
it "without advertiser_id should render 'new'" do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => nil,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) } }
expect(response).to render_template :new
end
it "without image should render 'new'" do
post :create, params: { ad: { 'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => nil } }
expect(response).to render_template :new
end
end
describe 'PATCH #update' do
before do
@ad = FactoryBot.create(:ad)
end
context 'when something changed' do
it 'with nothing should be redirected' do
patch :update, params: { ad: {
'text' => nil,
'price' => nil,
'advertiser_id' => nil,
'image' => nil
},id: @ad.id }
expect(response).to render_template :edit
end
it 'with text should be redirected' do
patch :update, params: { ad: { 'text' => 'Updated!' }, id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end
it 'with price should be redirected' do
patch :update, params: { ad: { 'price' => 1010101 }, id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end
it 'with advertiser_id should be redirected' do
patch :update, params: { ad: { 'advertiser_id' => 1010101 }, id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end
it 'with image should be redirected' do
patch :update, params: { ad: { 'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg')) }, id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end
it 'with all should be redirected' do
patch :update, params: { ad: {
'text' => 'Updated!',
'price' => 1010101,
'advertiser_id' => 1010101,
'image' => Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec/fixture/image.jpg'))
},id: @ad.id }
expect(response).to redirect_to(ad_index_path)
end
end
context 'when something empty' do
it 'without text should be rendered' do
patch :update, params: { ad: { 'text' =>nil }, id: @ad.id }
expect(response).to render_template :edit
end
it 'without price should be rendered' do
patch :update, params: { ad: { 'price' => nil }, id: @ad.id }
expect(response).to render_template :edit
end
it 'without advertiser_id should be rendered' do
patch :update, params: { ad: { 'advertiser_id' => nil }, id: @ad.id }
expect(response).to render_template :edit
end
it 'without image should be rendered' do
patch :update, params: { ad: { 'image' => nil }, id: @ad.id }
expect(response).to render_template :edit
end
it 'without image should be rendered' do
patch :update, params: { ad: {
'text' => nil,
'price' => nil,
'advertiser_id' => nil,
'image' => nil
},id: @ad.id }
expect(response).to render_template :edit
end
end
end
describe 'DELETE #destroy' do
before do
@ad = FactoryBot.create(:ad)
end
it 'should be deleted' do
expect { delete :destroy, params: { id: @ad.id } }.to change(Ad, :count).by(-1)
expect(response).to redirect_to(ad_index_path)
end
end
end