This commit is contained in:
João Victor Geonizeli
2022-02-27 17:02:37 -03:00
parent 53c7d35d74
commit 1d692b31f8
13 changed files with 258 additions and 3 deletions

7
spec/factories/posts.rb Normal file
View File

@@ -0,0 +1,7 @@
FactoryBot.define do
factory :post do
content { Faker::Lorem.paragraph }
user
quoted_post { nil }
end
end

31
spec/models/post_spec.rb Normal file
View File

@@ -0,0 +1,31 @@
require 'rails_helper'
RSpec.describe Post, type: :model do
describe '#kind' do
context 'when post have other post reference and a content' do
it 'returns :quoted_post' do
quoted_post = create(:post)
post = create(:post, quoted_post: quoted_post)
expect(post.kind).to eq(:quoted_post)
end
end
context 'when post have other post reference and dont have a content' do
it 'returns :repost' do
quoted_post = create(:post)
post = create(:post, quoted_post: quoted_post, content: nil)
expect(post.kind).to eq(:repost)
end
end
context 'when post dont have other post reference and have a content' do
it 'returns :post' do
post = create(:post)
expect(post.kind).to eq(:post)
end
end
end
end

View File

@@ -0,0 +1,88 @@
require 'rails_helper'
# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to test the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator. If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails. There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
RSpec.describe "/posts", type: :request do
let(:quoted_post) { create(:post) }
let(:valid_attributes) {
{
content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum.",
user_id: create(:user).id,
}
}
let(:invalid_attributes) {
{
content: "Quo dolorem recusandae. Vero laborum deleniti. Qui ipsam illum."
}
}
# This should return the minimal set of values that should be in the headers
# in order to pass any filters (e.g. authentication) defined in
# PostsController, or in your router and rack
# middleware. Be sure to keep this updated too.
let(:valid_headers) {
{}
}
describe "GET /index" do
it "renders a successful response" do
Post.create! valid_attributes
get posts_url, headers: valid_headers, as: :json
expect(response).to be_successful
end
end
describe "GET /show" do
it "renders a successful response" do
post = Post.create! valid_attributes
get post_url(post), as: :json
expect(response).to be_successful
end
end
describe "POST /create" do
context "with valid parameters" do
it "creates a new Post" do
expect {
post posts_url,
params: { post: valid_attributes }, headers: valid_headers, as: :json
}.to change(Post, :count).by(1)
end
it "renders a JSON response with the new post" do
post posts_url,
params: { post: valid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:created)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
context "with invalid parameters" do
it "does not create a new Post" do
expect {
post posts_url,
params: { post: invalid_attributes }, as: :json
}.to change(Post, :count).by(0)
end
it "renders a JSON response with errors for the new post" do
post posts_url,
params: { post: invalid_attributes }, headers: valid_headers, as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to match(a_string_including("application/json"))
end
end
end
end

View File

@@ -0,0 +1,18 @@
require "rails_helper"
RSpec.describe PostsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(get: "/posts").to route_to("posts#index")
end
it "routes to #show" do
expect(get: "/posts/1").to route_to("posts#show", id: "1")
end
it "routes to #create" do
expect(post: "/posts").to route_to("posts#create")
end
end
end