Reputation: 27
Here is my unit testing code for devise. Are there any ways to write better test code?
user_spec.rb:
require 'rails_helper'
RSpec.describe User, type: :model do
it { should have_many(:companies) }
it { should have_many(:jobs) }
it do
should validate_length_of(:encrypted_password).
is_at_least(6).
on(:create)
end
it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:encrypted_password) }
it { should have_db_column(:id) }
it { should have_db_column(:email) }
it { should have_db_column(:encrypted_password) }
it { should have_db_column(:confirmation_token) }
it { should have_db_column(:confirmed_at) }
it { should have_db_column(:confirmation_sent_at) }
it 'is databse authenticable' do
user = User.create(
email: '[email protected]',
password: 'password123',
password_confirmation: 'password123'
)
expect(user.valid_password?('password123')).to be_truthy
end
end
Upvotes: 0
Views: 189
Reputation: 734
Okay – as I understand it you're asking for what the best practices are for testing a Rails model. The fact that it's auto-generated by Devise is largely irrelevant.
The first thing I would consider here is what it is that you're actually testing – most of your code here is testing the underlying implementation of Devise. That shouldn't be the responsibility of your application's test suite – the library has a bunch of tests that should test that for you.
The only real useful tests here, IMO, are the association tests (which look like they come from shoulda-matchers
). The rest of the assertions here are testing Devise-specific code, which is tightly coupling your test suite to a 3rd-party library – that's only going to put you in a world of pain further down the line.
Upvotes: 2