Refactor unit tests for contents controller

- Creates different traits for contents on rspec and cucumber. This is
  needed because rspec serializes the file when it is passed as a
  parameter.
- Remove json reponses for content controller
Signed-off-by: marcheing's avatarHeitor Reis <marcheing@gmail.com>
parent 12338097
Pipeline #3598797 failed with stage
in 8 minutes and 51 seconds
......@@ -20,10 +20,8 @@ class ContentsController < ApplicationController
respond_to do |format|
if @content.save
format.html { redirect_to @content, notice: I18n.t('successfully_created_video') }
format.json { render :show, status: :created, location: @content }
else
format.html { render :new }
format.json { render json: @content.errors, status: :unprocessable_entity }
end
end
end
......
......@@ -19,68 +19,68 @@ require 'rails_helper'
# that an instance is receiving a specific message.
RSpec.describe ContentsController, type: :controller do
include Devise::TestHelpers
# This should return the minimal set of attributes required to create a valid
# Content. As you add validations to Content, be sure to
# adjust the attributes here as well.
let(:valid_attributes) {
skip("Add a hash of attributes valid for your model")
let(:valid_request_parameters) {
attributes_for(:video)
}
let(:invalid_attributes) {
skip("Add a hash of attributes invalid for your model")
}
let(:user) { create(:user, confirmed_at: Time.zone.now) }
before do
sign_in user
end
# This should return the minimal set of values that should be in the session
# in order to pass any filters (e.g. authentication) defined in
# ContentsController. Be sure to keep this updated too.
let(:valid_session) { {} }
describe 'GET #show' do
let(:video) { build(:video, :rspec, id: 1) }
context 'video found' do
before do
expect(Content).to receive(:find).with(video.id.to_s).and_return(video)
get :show, params: { id: video.id }
end
describe "GET #show" do
it "assigns the requested content as @content" do
content = Content.create! valid_attributes
get :show, params: {id: content.to_param}, session: valid_session
expect(assigns(:content)).to eq(content)
it { is_expected.to respond_with(:ok) }
end
end
describe "GET #new" do
it "assigns a new content as @content" do
get :new
expect(response.status).to eq 200
context 'video not found' do
before do
expect(Content).to receive(:find).with(video.id.to_s).and_raise ActiveRecord::RecordNotFound
end
it 'responds with not found' do
pending 'Handle not found errors'
get :show, params: { id: video.id }
it { is_expected.to respond_with(:not_found) }
end
end
end
describe "POST #create" do
context "with valid params" do
it "creates a new Content" do
expect {
post :create, params: {content: valid_attributes}, session: valid_session
}.to change(Content, :count).by(1)
end
describe 'GET #new' do
it 'responds with ok' do
get :new
expect(response.status).to eq 200
end
end
it "assigns a newly created content as @content" do
post :create, params: {content: valid_attributes}, session: valid_session
expect(assigns(:content)).to be_a(Content)
expect(assigns(:content)).to be_persisted
describe 'POST #create' do
let(:video) { build(:video, with: :rspec) }
context 'with valid params' do
before do
expect_any_instance_of(Content).to receive(:save).and_return true
post :create, params: { content: valid_request_parameters }
end
it "redirects to the created content" do
post :create, params: {content: valid_attributes}, session: valid_session
expect(response).to redirect_to(Content.last)
it 'redirects to the created content' do
expect(response).to redirect_to(Content.last)
end
end
context "with invalid params" do
it "assigns a newly created but unsaved content as @content" do
post :create, params: {content: invalid_attributes}, session: valid_session
expect(assigns(:content)).to be_a_new(Content)
context 'with invalid params' do
before do
expect_any_instance_of(Content).to receive(:save).and_return false
post :create, params: { content: valid_request_parameters }
end
it "re-renders the 'new' template" do
post :create, params: {content: invalid_attributes}, session: valid_session
expect(response).to render_template("new")
end
it { is_expected.to respond_with(:ok) }
end
end
end
FactoryGirl.define do
factory :video, class: Content do
title 'Video Title'
video "#{Rails.root}/public/cucumber/videos/video.mp4"
zip_code '00000-000'
soundtrack 'soundtrack'
end
trait :rspec do
video_file_name "#{Rails.root}/public/cucumber/videos/video.mp4"
end
trait :cucumber do
video { File.new "#{Rails.root}/public/cucumber/videos/video.mp4" }
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment