add user scaffold

This commit is contained in:
João Victor Geonizeli
2022-02-27 14:46:30 -03:00
parent cec57e0ad2
commit 011a8cdb77
16 changed files with 163 additions and 19 deletions

5
spec/factories/users.rb Normal file
View File

@@ -0,0 +1,5 @@
FactoryBot.define do
factory :user do
username { "xpto" }
end
end

36
spec/models/user_spec.rb Normal file
View File

@@ -0,0 +1,36 @@
require 'rails_helper'
RSpec.describe User, type: :model do
describe 'validations' do
describe 'username' do
it 'presence' do
user = build(:user, username: nil)
expect(user).to_not be_valid
end
it 'uniqueness' do
create(:user, username: 'xpto')
user = build(:user, username: 'xpto')
expect(user).to_not be_valid
end
it 'max length 14' do
with_15_caracters = build(:user, username: 'asdfghjklzxcvhn')
with_14_caracters = build(:user, username: 'asdfghjklzxcvh')
expect(with_15_caracters).to_not be_valid
expect(with_14_caracters).to be_valid
end
it 'format' do
with_invalid_format = build(:user, username: 'asdfghjklzxcvh!')
with_valid_format = build(:user, username: 'asdfghjklzxcvh')
expect(with_invalid_format).to_not be_valid
expect(with_valid_format).to be_valid
end
end
end
end

View File

@@ -34,6 +34,8 @@ RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.include FactoryBot::Syntax::Methods
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
@@ -62,10 +64,3 @@ RSpec.configure do |config|
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

View File

@@ -0,0 +1,17 @@
require 'rails_helper'
RSpec.describe "/users", type: :request do
let(:valid_attributes) {
{
username: 'geonizeli'
}
}
describe "GET /show" do
it "renders a successful response" do
user = User.create! valid_attributes
get user_url(user), as: :json
expect(response).to be_successful
end
end
end

View File

@@ -0,0 +1,9 @@
require "rails_helper"
RSpec.describe UsersController, type: :routing do
describe "routing" do
it "routes to #show" do
expect(get: "/users/1").to route_to("users#show", id: "1")
end
end
end