-
Notifications
You must be signed in to change notification settings - Fork 15
Homework #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Homework #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 2.4.0 | ||
| 2.6.3 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| FactoryBot.define do | ||
| # Implementar factory | ||
| factory :post do | ||
| association :user | ||
|
|
||
| title { Faker::Lorem.unique.sentence } | ||
| body { Faker::Lorem.paragraph } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,28 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe Post, type: :model do | ||
| # En base al modelo de test propuesto para el modelo User, | ||
| # implementar los tests para el modelo Post. | ||
| describe 'Factory' do | ||
| subject { build(:post) } | ||
|
|
||
| it 'has a valid factory' do | ||
| is_expected.to be_valid | ||
| end | ||
| end | ||
|
|
||
| describe 'Associations' do | ||
| it { should belong_to(:user) } | ||
| end | ||
|
|
||
| describe 'Presence validations' do | ||
| it { should validate_presence_of(:title) } | ||
| it { should validate_presence_of(:body) } | ||
| end | ||
|
|
||
| describe 'Uniqueness validations' do | ||
| subject { create(:post) } | ||
|
|
||
| it { should validate_uniqueness_of(:title) } | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'rails_helper' | ||
|
|
||
| RSpec.describe User, type: :model do | ||
| describe 'Factory' do | ||
| subject { build(:user) } | ||
|
|
||
| it 'has a valid factory' do | ||
| # Testear que el factory definido es válido. | ||
| is_expected.to be_valid | ||
| end | ||
| end | ||
|
|
||
| describe 'Associations' do | ||
| # Testear asociaciones (shoulda-matchers). | ||
| # https://github.com/thoughtbot/shoulda-matchers#activerecord-matchers | ||
| it { should have_many(:posts).dependent(:destroy) } | ||
| end | ||
|
|
||
| describe 'Presence validations' do | ||
| # Testear validaciones de presencia (shoulda-matchers). | ||
| # https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers | ||
| it { should validate_presence_of(:email) } | ||
| it { should validate_presence_of(:password) } | ||
| it { should validate_presence_of(:role) } | ||
| end | ||
|
|
||
| describe 'Uniqueness validations' do | ||
| # Testear validaciones de unicidad (shoulda-matchers). | ||
| # https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers | ||
| subject { create(:user) } | ||
|
|
||
| it { should validate_uniqueness_of(:email) } | ||
| end | ||
|
|
||
| describe 'Length validations' do | ||
| # Testear validaciones de longitud (shoulda-matchers). | ||
| # https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers | ||
| it { should validate_length_of(:password).is_at_least(8) } | ||
| end | ||
|
|
||
| describe 'Enumeratives' do | ||
| # Testear definición de enumerativos (shoulda-matchers). | ||
| it { should define_enum_for(:role).with_values([:admin, :regular]) } | ||
| end | ||
|
|
||
| # Testear métodos de instancia y de clase como para el caso de cualquier | ||
| # otra clase Ruby (similar a testear ApiLoginManager). | ||
| describe '#valid_password?' do | ||
| # Testear funcionamiento de método. Podemos definir dos contexts: | ||
| # - 'when given value is different from password' | ||
| # - 'when given value is equal to password' | ||
| subject { user.valid_password?(password) } | ||
|
|
||
| let(:user) { create(:user, password: 'password_secret') } | ||
|
|
||
| context 'when given value is different from password' do | ||
| let(:password) { 'password_wrong' } | ||
|
|
||
| it { is_expected.to be_falsey } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Está bien, pero sabemos que si falla el resultado es |
||
| end | ||
|
|
||
| context 'when given value is equal to password' do | ||
| let(:password) { 'password_secret' } | ||
|
|
||
| it { is_expected.to be_truthy } | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Lo mismo acá, sabemos que debería retornar |
||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pese a que el test pasa y está bien, podemos usar un
build_stubbedpara no persistir alPosten la base de datos (para mejorar la performance). Elbuildes mejor en este sentido que elcreatey lo veo muy bien, pero aún así persiste aquellas asociaciones que tenga definido el factory. Es sólo un detalle, pero está bueno conocerlo.