-
Notifications
You must be signed in to change notification settings - Fork 15
homework 1 #5
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 1 #5
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,3 +1,7 @@ | ||
| FactoryBot.define do | ||
| # Implementar factory | ||
| factory :post do | ||
| association :user | ||
| title { Faker::Lorem.unique.word } | ||
| body { Faker::Lorem.sentence(30) } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,37 +3,51 @@ | |
| RSpec.describe User, type: :model do | ||
| describe 'Factory' do | ||
| it 'has a valid factory' do | ||
| # Testear que el factory definido es válido. | ||
| expect(build(:user)).to be_valid | ||
| end | ||
| end | ||
|
|
||
| describe 'Associations' do | ||
| # Testear asociaciones (shoulda-matchers). | ||
| it { should have_many(:posts) } | ||
|
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. Podemos agregar el test del describe 'Associations' do
it { should have_many(:posts).dependent(:destroy) }
end |
||
| # https://github.com/thoughtbot/shoulda-matchers#activerecord-matchers | ||
| end | ||
|
|
||
| describe 'Presence validations' do | ||
| # Testear validaciones de presencia (shoulda-matchers). | ||
| it { should validate_presence_of(:email) } | ||
| it { should validate_presence_of(:password) } | ||
| it { should validate_presence_of(:role) } | ||
| # https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers | ||
| end | ||
|
|
||
| describe 'Uniqueness validations' do | ||
| # Testear validaciones de unicidad (shoulda-matchers). | ||
| subject { create(:user) } | ||
| it { should validate_uniqueness_of(:email) } | ||
| # https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers | ||
| end | ||
|
|
||
| describe 'Length validations' do | ||
| # Testear validaciones de longitud (shoulda-matchers). | ||
| it { should validate_length_of(:password).is_at_least(8) } | ||
| # https://github.com/thoughtbot/shoulda-matchers#activemodel-matchers | ||
| end | ||
|
|
||
| describe 'Enumeratives' do | ||
| # Testear definición de enumerativos (shoulda-matchers). | ||
| it { should define_enum_for(:role) } | ||
| 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 | ||
| subject { create(:user) } | ||
| context 'when given value is different from password' do | ||
| it 'returns false' do | ||
| expect(subject.valid_password?(SecureRandom.uuid)).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 sabiendo que el resultado sería directamente |
||
| end | ||
| end | ||
|
|
||
| context 'when given value is equal to password' do | ||
| it 'returns true' do | ||
| expect(subject.valid_password?(subject.password)).to be | ||
| end | ||
| end | ||
|
|
||
| # Testear funcionamiento de método. Podemos definir dos contexts: | ||
| # - 'when given value is different from password' | ||
| # - 'when given value is equal to password' | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,11 +11,12 @@ | |
| # en let!(:user) { ... }, usar esta variable. | ||
| let!(:password) { SecureRandom.hex } | ||
|
|
||
| let!(:user) { } # -- crear usuario con FactoryBot -- # | ||
| let!(:user) { create(:user, password: password) } # -- crear usuario con FactoryBot -- # | ||
|
|
||
| describe '#call' do | ||
| context 'when user provided data is valid' do | ||
| before do | ||
| allow(ExternalValidator).to receive(:call).and_return(true) | ||
| # Realizar un stub del ExternalValidator para que se ejecuta realmente | ||
| end | ||
|
|
||
|
|
@@ -24,60 +25,87 @@ | |
| end | ||
|
|
||
| it "update user's auth_token" do | ||
| skip 'Una vez que definan a user, este test va a fallar. ¿Por qué?' | ||
| expect { subject }.to change { user.auth_token } | ||
| expect { subject }.to change { user.reload.auth_token } | ||
| end | ||
|
|
||
| it 'returns the auth_token' do | ||
| skip 'Implementar' | ||
| expect(subject).to eq(user.reload.auth_token) | ||
| end | ||
| end | ||
|
|
||
| context 'when no email is provided' do | ||
| subject { } # implementar | ||
| subject { described_class.new(password: password) } | ||
|
|
||
| it 'returns false' do | ||
| skip 'Implementar' | ||
| expect(subject.call).to be_falsey | ||
| end | ||
|
|
||
| it 'returns EMPTY_EMAIL error with a reader' do | ||
| skip 'Implementar' | ||
| subject.call | ||
| expect(subject.error).to be(ApiLoginManager::EMPTY_EMAIL) | ||
| end | ||
| end | ||
|
|
||
| context 'when no password is provided' do | ||
| subject { } # implementar | ||
| subject { described_class.new(email: user.email) } | ||
|
|
||
| it 'returns false' do | ||
| skip 'Implementar' | ||
| expect(subject.call).to be_falsey | ||
| end | ||
|
|
||
| it 'returns EMPTY_PASSWORD error with a reader' do | ||
| skip 'Implementar' | ||
| subject.call | ||
| expect(subject.error).to be(ApiLoginManager::EMPTY_PASSWORD) | ||
| end | ||
| end | ||
|
|
||
| context 'when the email is incorrect' do | ||
| subject { } # implementar | ||
| subject do | ||
| described_class.new( | ||
| email: Faker::Internet.unique.email, | ||
| password: user.password | ||
| ) | ||
| end | ||
|
|
||
| it 'returns false' do | ||
| skip 'Implementar' | ||
| expect(subject.call).to be_falsey | ||
| end | ||
|
|
||
| it 'returns USER_NOT_FOUND error with a reader' do | ||
| skip 'Implementar' | ||
| subject.call | ||
| expect(subject.error).to be(ApiLoginManager::USER_NOT_FOUND) | ||
| end | ||
| end | ||
|
|
||
| context 'when the password is incorrect' do | ||
| subject { } # implementar | ||
| subject do | ||
| described_class.new(email: user.email, password: SecureRandom.uuid) | ||
| end | ||
|
|
||
| it 'returns false' do | ||
| skip 'Implementar' | ||
| expect(subject.call).to be_falsey | ||
| end | ||
|
|
||
| it 'returns WRONG_PASSWORD error with a reader' do | ||
| skip 'Implementar' | ||
| subject.call | ||
| expect(subject.error).to be(ApiLoginManager::WRONG_PASSWORD) | ||
| end | ||
| end | ||
|
|
||
| context 'when ExternalValidator fails' do | ||
| before do | ||
| allow(ExternalValidator).to receive(:call).and_return(false) | ||
| end | ||
|
|
||
| subject { described_class.new(email: user.email, password: user.password) } | ||
|
|
||
| it 'returns false' do | ||
| expect(subject.call).to be_falsey | ||
| end | ||
|
|
||
| it 'returns EXTERNAL_VALIDATOR error with a reader' do | ||
| subject.call | ||
| expect(subject.error).to be(ApiLoginManager::EXTERNAL_VALIDATOR) | ||
| end | ||
| end | ||
|
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. Creo que modificaría los |
||
|
|
||
|
|
||
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.