Skip to content

Commit a174569

Browse files
committed
Consolidate specs
1 parent 0264eac commit a174569

File tree

12 files changed

+199
-155
lines changed

12 files changed

+199
-155
lines changed

app/models/training_resource.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
class TrainingResource < ApplicationRecord
22
belongs_to :topic
3+
has_one :language, through: :topic
34
has_one_attached :document
45

56
validates :state, presence: true
7+
68
validates_with DocumentValidator
79
validates_with ResourceLanguageValidator
810
end

spec/factories/providers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FactoryBot.define do
22
factory :provider do
3-
name { "ACME" }
3+
sequence(:name) { |n| "provider_#{n}" }
44
provider_type { "provider" }
55
end
66
end
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
FactoryBot.define do
22
factory :training_resource do
3-
state { 1 }
3+
sequence(:file_name_override) { |n| "file_name_override_#{n}.jpg" }
44
document { Rack::Test::UploadedFile.new("spec/support/images/logo_ruby_for_good.png", "image/png") }
5-
topic { create(:topic) }
6-
file_name_override { "test.jpg" }
5+
state { 1 }
6+
topic
77
end
88
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "POST /training_resources" do
5+
let(:user) { create(:user) }
6+
let(:topic) { create(:topic) }
7+
let(:valid_attributes) { attributes_for(:training_resource, topic_id: topic.id) }
8+
let(:invalid_attributes) { { document: "", file_name_override: "", state: "", topic_id: "" } }
9+
10+
before { sign_in(user) }
11+
12+
context "with valid parameters" do
13+
it "creates a new TrainingResource" do
14+
expect {
15+
post training_resources_url, params: { training_resource: valid_attributes }
16+
}.to change(TrainingResource, :count).by(1)
17+
end
18+
19+
it "redirects to the created training_resource" do
20+
post training_resources_url, params: { training_resource: valid_attributes }
21+
expect(response).to redirect_to(training_resource_url(TrainingResource.last))
22+
end
23+
end
24+
25+
context "with invalid parameters" do
26+
it "does not create a new TrainingResource" do
27+
expect {
28+
post training_resources_url, params: { training_resource: invalid_attributes }
29+
}.to change(TrainingResource, :count).by(0)
30+
end
31+
32+
it "renders a response with 422 status (i.e. to display the 'new' template)" do
33+
post training_resources_url, params: { training_resource: invalid_attributes }
34+
expect(response).to have_http_status(:unprocessable_entity)
35+
end
36+
end
37+
38+
context "with file_name_override present" do
39+
it "does not create a new TrainingResource if there is already a preexistent entry with the same language" do
40+
preexistent_topic = create(:topic, language_id: topic.language_id)
41+
TrainingResource.create! valid_attributes
42+
43+
post training_resources_url, params: { training_resource: valid_attributes.merge(topic_id: preexistent_topic.id) }
44+
45+
expect(response).to have_http_status(:unprocessable_entity)
46+
end
47+
48+
it "creates a new Training Resource if file_name_override has a different language" do
49+
preexistent_topic = create(:topic)
50+
TrainingResource.create! valid_attributes
51+
52+
post training_resources_url, params: { training_resource: valid_attributes.merge(topic_id: preexistent_topic.id) }
53+
54+
expect(response).to have_http_status(:redirect)
55+
end
56+
end
57+
end
58+
end
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "DELETE /destroy" do
5+
let(:user) { create(:user) }
6+
let(:topic) { create(:topic) }
7+
let(:valid_attributes) { attributes_for(:training_resource, topic_id: topic.id) }
8+
9+
before { sign_in(user) }
10+
11+
it "destroys the requested training_resource" do
12+
training_resource = TrainingResource.create! valid_attributes
13+
expect {
14+
delete training_resource_url(training_resource)
15+
}.to change(TrainingResource, :count).by(-1)
16+
end
17+
18+
it "redirects to the training_resources list" do
19+
training_resource = TrainingResource.create! valid_attributes
20+
delete training_resource_url(training_resource)
21+
expect(response).to redirect_to(training_resources_url)
22+
end
23+
end
24+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "GET /training_resources/:id" do
5+
let(:user) { create(:user) }
6+
7+
before { sign_in(user) }
8+
9+
it "renders a successful response" do
10+
training_resource = create(:training_resource)
11+
12+
get edit_training_resource_url(training_resource)
13+
14+
expect(response).to be_successful
15+
end
16+
end
17+
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "GET /training_resources" do
5+
let(:user) { create(:user) }
6+
7+
before { sign_in(user) }
8+
9+
it "renders a successful response" do
10+
create(:training_resource)
11+
12+
get training_resources_url
13+
14+
expect(response).to be_successful
15+
expect(assigns(:training_resources)).to eq(TrainingResource.all)
16+
end
17+
end
18+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "GET /training_resources/new" do
5+
let(:user) { create(:user) }
6+
7+
before { sign_in(user) }
8+
9+
it "renders a successful response" do
10+
get new_training_resource_url
11+
12+
expect(response).to be_successful
13+
end
14+
end
15+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "GET /training_resources/:id" do
5+
let(:user) { create(:user) }
6+
7+
before { sign_in(user) }
8+
9+
it "renders a successful response" do
10+
training_resource = create(:training_resource)
11+
12+
get training_resource_url(training_resource)
13+
14+
expect(response).to be_successful
15+
end
16+
end
17+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "Training Resources", type: :request do
4+
describe "PATCH /training_resources" do
5+
let(:user) { create(:user) }
6+
let(:topic) { create(:topic) }
7+
let(:valid_attributes) { attributes_for(:training_resource, topic_id: topic.id) }
8+
let(:invalid_attributes) { { document: "", file_name_override: "", state: "", topic_id: "" } }
9+
10+
before { sign_in(user) }
11+
12+
context "with valid parameters" do
13+
let(:new_attributes) {
14+
{ state: 1 }
15+
}
16+
17+
it "updates the requested training_resource" do
18+
training_resource = TrainingResource.create! valid_attributes
19+
patch training_resource_url(training_resource), params: { training_resource: new_attributes }
20+
training_resource.reload
21+
expect(response).to redirect_to(training_resource_url(training_resource))
22+
end
23+
24+
it "redirects to the training_resource" do
25+
training_resource = TrainingResource.create! valid_attributes
26+
patch training_resource_url(training_resource), params: { training_resource: new_attributes }
27+
training_resource.reload
28+
expect(response).to redirect_to(training_resource_url(training_resource))
29+
end
30+
end
31+
32+
context "with invalid parameters" do
33+
it "renders a response with 422 status (i.e. to display the 'edit' template)" do
34+
training_resource = TrainingResource.create! valid_attributes
35+
patch training_resource_url(training_resource), params: { training_resource: invalid_attributes }
36+
expect(response).to have_http_status(:unprocessable_entity)
37+
end
38+
end
39+
end
40+
end

0 commit comments

Comments
 (0)